LNK errors (LNK2019 and LNK1120)

Hi, I create a new project with precompiled header
here my project

Complex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Complex class declaration

#include "stdafx.h"

#ifndef COMPLEX_H
#define COMPLEX_H

class Complex
{
private:
	float real,imagine;
public:
	Complex();
	Complex(float r, float i);
	void input();
	void print();
};

#endif 


Complex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//Complex class member-function definitions

#include "stdafx.h"
#include "iostream"
using std::cout;
using std::cin;
using std::endl;

#include "Complex.h"

Complex::Complex(float r, float i)
{
	real=r;
	imagine=i;
}

void Complex::input()
{
	cout<<endl;
	cout<<"Enter real part: ";
	cin>>real;
	cout<<"Enter imaginary part: ";
	cin>>imagine;
}

void Complex::print()
{
	cout<<"Your complex number"<<endl;
	cout<<real<<" + "<<imagine<<"i"<<endl;
}


Main program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Main program
#include "stdafx.h"
#include "iostream"
using std::cout;
using std::cin;
using std::endl;

#include "Complex.h"

int main()
{
	Complex a;
	a.input();
	a.print();

	return 0;
}


When I press Ctrl + F5 for debug, some errors occured
1
2
1>1-Complex.obj : error LNK2019: unresolved external symbol "public: __thiscall Complex::Complex(void)" (??0Complex@@QAE@XZ) referenced in function _main
1>D:\Study\Tran Van Tuan\University\Subjects\OOP\Thuc Hanh OOP\Projects\1\1-Complex\Debug\1-Complex.exe : fatal error LNK1120: 1 unresolved externals


When I turn on the "Display all progress message" in Project Properties > Linker > General > Show Progress, the error was here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...blah blah blah
1>      Searching C:\Program Files\Microsoft SDKs\Windows\v7.0A\lib\odbc32.lib:
1>      Searching C:\Program Files\Microsoft SDKs\Windows\v7.0A\lib\odbccp32.lib:
1>      Searching C:\Program Files\Microsoft Visual Studio 10.0\VC\lib\msvcprtd.lib:
1>      Searching C:\Program Files\Microsoft Visual Studio 10.0\VC\lib\MSVCRTD.lib:
1>      Searching C:\Program Files\Microsoft Visual Studio 10.0\VC\lib\OLDNAMES.lib:
1>  
1>  Finished searching libraries
1>  
1>  Finished pass 1
1>  
1>  Generating non-SAFESEH image.
1>1-Complex.obj : error LNK2019: unresolved external symbol "public: __thiscall Complex::Complex(void)" (??0Complex@@QAE@XZ) referenced in function _main
1>D:\Study\Tran Van Tuan\University\Subjects\OOP\Thuc Hanh OOP\Projects\1\1-Complex\Debug\1-Complex.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.03
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
As far as I know you shouldn't include the precompiled header in another header. Since precompiled header are useless I'd suggest to switch them off.

But that's not the point. The point is that you haven't implemented the constructor the compiler is complaining about. In 'Complex.cpp' there should be something like this:
1
2
3
4
5
Complex::Complex() :
	real(0)
	imagine(0)
{
}
Thanks coder777. It's exactly the problem. I just forget to define the constructor

Your code have some problem
1
2
3
4
Complex::Complex()
           : real(0), //there is a comma here
           imagine(0)
{}


Thanks again
Topic archived. No new replies allowed.