Implementation File and Driver won't link

Hello, I am one of the many amateurs at programming c++ and I have a problem with a project in which the implementation file requires a main driver even though I have a driver file in the same folder.

complex.h:
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
31
32
33
34
35
36
37
38
39
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>

using namespace std;

class Complex
{
public:
	Complex();
	Complex(double realPart);
	Complex(double real, double imaginary);
	
	double getr();
	double geti();
	
	void set(double real, double imaginary);
	
	Complex operator -() const;
	Complex operator -(const Complex& comp) const;
	Complex operator +(const Complex& comp) const;
	
	Complex operator *(const Complex& comp) const;
	Complex operator /(const Complex& comp) const;

	bool operator ==(const Complex& comp);

	friend std::ostream& operator <<(ostream& lhs, const Complex& rhs);
	friend std::istream& operator >>(istream& lhs, Complex& rhs);

	Complex sqrt ( Complex comp, int method = 0, int iteration = 0) const;
	
	Complex abs ( Complex c);

private:
	double real, imaginary;
};

#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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "complex.h"
#include <iostream>
#include <math.h>

using namespace std;

const Complex i(0,1);

Complex::Complex ()
{
	set(0,0);
}

Complex::Complex ( double realPart )
{
	set(realPart,0);
}

Complex::Complex ( double real, double imaginary )
{
	set(real,imaginary);
}

double Complex::getr()
{
	return real;
}

double Complex::geti()
{
	return imaginary;
}

void Complex::set(double real, double imaginary)
{
	this->real = real;
	this->imaginary = imaginary;
}

Complex Complex::operator -() const
{
	return Complex(real,-imaginary);
}

Complex Complex::operator -(const Complex& comp) const
{
	double subReal,subImaginary;
	subReal = real-comp.real;
	subImaginary = imaginary-comp.imaginary;
	return Complex(subReal,subImaginary);
}

Complex Complex::operator +(const Complex& comp) const
{
	double sumReal,sumImaginary;
	sumReal = real+comp.real;
	sumImaginary = imaginary+comp.imaginary;
	return Complex(sumReal,sumImaginary);
}

Complex Complex::operator *(const Complex& comp) const
{
	double prodReal,prodimaginary;
	prodReal = real*comp.real - imaginary*comp.imaginary;
	prodimaginary = real*comp.imaginary + imaginary*comp.real;
	return Complex(prodReal,prodimaginary);
}

Complex Complex::operator /(const Complex& comp) const
{
	double quotReal,quotimaginary;

	Complex numer;
	Complex denom;
	numer = *this*(-comp);
	denom = comp*(-comp);

	quotReal = numer.real/denom.real;
	quotimaginary = numer.imaginary/denom.real;
	return Complex(quotReal,quotimaginary);
}

bool Complex::operator ==(const Complex& comp)
{
	return (real == comp.real)&&(imaginary == comp.imaginary);
}

ostream& operator<< (ostream& lhs, Complex& rhs)
{
	lhs << rhs.getr() << "+" << rhs.geti() << "i";
	return lhs;
}

istream& operator>> (istream& lhs, Complex& rhs)
{
	lhs >> rhs.real >> rhs.imaginary;
	return lhs;
}

Complex Complex::sqrt ( Complex comp, int method, int iteration) const
{
	double srReal,srImaginary;
	if(method == 0)
	{
		srReal = pow( pow( pow(comp.real,2)+pow(comp.imaginary,2), 0.5 ) + comp.real, 0.5 )/pow(2.0, 0.5);
		srImaginary = pow( pow( pow(comp.real,2)+pow(comp.imaginary,2), 0.5 ) - comp.real, 0.5 )/pow(2.0, 0.5);
		if(comp.imaginary < 0)
			srImaginary *= -1;
	}
	return Complex(srReal,srImaginary);
}

Complex Complex::abs ( Complex c)
{
	if(c.real < 0)
		c.real = -c.real;
	if(c.imaginary < 0)
		c.imaginary = -c.imaginary;
	return Complex(c.real,c.imaginary);
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "complex.h"
#include <iostream>
#include <math.h>

using namespace std;

int	main ()
{
	Complex a(5,3),b(7,4),c;
	c = a*b;
	cout << c.getr() << endl << c.geti() << endl << a.getr() << endl << a.geti() << endl;
	c = a+b;
	cout << c << endl;
	c = a/b;
	cout << c << endl;
	c = c.sqrt(a);
	cout << c << endl;
	cin >> c;
	cout << c << endl;
	return 0;
}


When I compile the implementation file, I get this error:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

complex.cpp
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:complex.exe
complex.obj
LIBCMT.lib(crt0.obj) : error LNK2019: unresolved external symbol _main reference
d in function ___tmainCRTStartup
complex.exe : fatal error LNK1120: 1 unresolved externals
Last edited on
Yes, it's likely the wrong type of project. Choose 'console' instead of just 'win32'
Topic archived. No new replies allowed.