Overloading

May 10, 2013 at 2:55am
I am positive I did this right but I can't figure out why when I compile I am getting a weird error.

header
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
  
#ifndef H_complexNumber
#define H_complexNumber

#include <iostream>
using namespace std;

class complexType
{
	friend ostream& operator<<(ostream&, const complexType&);
	friend istream& operator>>(istream&, const complexType&);

public:
	void setComplex(const double& real, const double& imag);
	void getComplex(double& real, double& imag) const;
	complexType(double real = 0, double imag = 0);
	complexType operator+ (const complexType& otherComplex) const;
	complexType operator* (const complexType& otherComplex) const;
	complexType operator- (const complexType& otherComplex) const;
	complexType operator/ (const complexType& otherComplex) const;
	bool operator== (const complexType& otherComplex) const;
	double realPart;
	double imaginaryPart;
	
};

#endif 


functions
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
#include "complexNumber.h"

ostream& operator<< (ostream& osObject, const complexType& complex)
{
	osObject << "(";
	osObject << complex.realPart;
	osObject << ", ";
	osObject << complex.imaginaryPart;
	osObject << ")";

	return osObject;
}

istream& operator>> (istream& isObject, complexType& complex)
{
	char ch;

	isObject >> ch;
	isObject >> complex.realPart;
	isObject >> ch;
	isObject >> complex.imaginaryPart;
	isObject >> ch;

	return isObject;
}

bool complexType::operator==(const complexType& otherComplex) const
{
	return (realPart == otherComplex.realPart && imaginaryPart == otherComplex.imaginaryPart);
}

complexType::complexType(double real, double imag)
{
	realPart = real;
	imaginaryPart = imag;
}

void complexType::setComplex(const double& real, const double& imag)
{
	realPart = real;
	imaginaryPart = imag;
}

void complexType::getComplex(double& real, double& imag) const
{
	real = realPart;
	imag = imaginaryPart;
}

complexType complexType::operator+(const complexType& otherComplex) const
{
	complexType temp;

	temp.realPart = realPart + otherComplex.realPart;
	temp.imaginaryPart = imaginaryPart + otherComplex.imaginaryPart;

	return temp;
}

complexType complexType::operator*(const complexType& otherComplex) const
{
	complexType temp;


Program
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

#include <iostream>
#include "complexNumber.h"

using namespace std;

int main()
{
	complexType num1 (23, 34);
	complexType num2;
	complexType num3;

	cout << "Num1 = " << num1 << endl;
	cout << "Num2 = " << num2 << endl;

	cout << "Enter the complex number "
		 << "in the form of (a, b) ";
	cin >> num2;
	cout << endl;

	cout << "New value of num2 = " << num2 << endl;
	
	num3 = num1 + num2;

	cout << "Num3 = " << num3 << endl;
	
	cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
	cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
	cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
	cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
	
	return 0;
}


Any idea?
May 10, 2013 at 3:36am
getting a weird error

And the error is...?
May 21, 2013 at 2:57am
Sorry for pulling this back up after 10 days, but the error is still bugging me. My teacher never responded on my help over the compile error. This is what I get when I compile it.

Ch 13 Number 7.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class complexType const &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@ABVcomplexType@@@Z) referenced in function _main

C:\Users\....\documents\visual studio 2010\Projects\Ch 13 Number 7\Debug\Ch 13 Number 7.exe : fatal error LNK1120: 1 unresolved externals
May 21, 2013 at 3:51am
Maybe because you are declaring on line 10 two objects but with no parameter if you look at your class you don't have a complexType() or complexType( void ) which you are trying to call.
May 21, 2013 at 6:39am
line 14 istream& operator>> (istream& isObject, complexType& complex) should be const istream& operator>> (istream& isObject, const complexType& complex)

line 29-30 you use the - and / operators but you didn't write a definition for them.
Last edited on May 21, 2013 at 6:55am
May 21, 2013 at 7:17am
Lin 11: friend istream& operator>>(istream&, const complexType&);
The input stream is going to write into the complexType so it cannot be const. Change to:

friend istream& operator>>(istream&, complexType&);

The linker error was because the prototype parameters didn't match your implementation. But don't change the implementation, change the prototype as above. If you change the implementation by adding const in front of the complexType parameter it shouldn't compile because the compiler will not allow you to modify complex.realPart and complex.imaginaryPart.
May 21, 2013 at 7:32am
Yeah your right I just meant the prototype and definition header didn't match up.
Last edited on May 21, 2013 at 7:33am
Topic archived. No new replies allowed.