Operator Overloading Error

Hello, I am somewhat new to C++ and trying out some operator overloading. I am getting some errors which I can not understand why. Here are what the errors look like:

1
2
3
4
5
1>c:\users\dave\desktop\ecs 60\longint\longint\longint.h(93): error C2556: 'LongInt &operator +(LongInt &,LongInt &)' : overloaded function differs only by return type from 'LongInt operator +(LongInt &,LongInt &)'

1>c:\users\dave\desktop\ecs 60\longint\longint\longint.h(32) : see declaration of 'operator +'

1>c:\users\dave\desktop\ecs 60\longint\longint\longint.h(93): error C2040: 'operator +' : 'LongInt &(LongInt &,LongInt &)' differs in levels of indirection from 'LongInt (LongInt &,LongInt &)'


If someone could help me fix these, that'd be great. Thanks

Here is my class:

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
class Node{
public:
	// Constructor and Destructor
	Node();
	~Node();
	
	// Variables
	void set(int);
	void setnext(Node *);
	void setback(Node *);
	int stuff;
	Node* forward;
	Node* backward;
};

class LongInt{
public:
	// Constructor and Destructor
	LongInt();
	~LongInt();
	
	// Overload Opertators
	friend istream& operator >>(istream& in, LongInt& longnumber);
	friend ostream& operator <<(ostream& out, LongInt& longnumber);
	friend LongInt operator +(LongInt& int1, LongInt& int2);

	// Variables
	void insertF(int);
	void insertB(int);
	Node *tail, *head;
};


And here is the operator being overloaded:

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
LongInt& operator +(LongInt& int1, LongInt& int2){
	Node *ptr1, *ptr2, *ptr3;
	LongInt *start = new LongInt;
	int answer = 0;
	bool carry = 0;

	ptr1 = int1.tail;
	ptr2 = int2.tail;

	while((ptr1 != NULL) && (ptr2 != NULL)){
		if(carry == 0){
			answer = ptr1->stuff + ptr2->stuff;
		}
		else{
			answer = ptr1->stuff + ptr2->stuff + 1;
		}

		if(answer > 9){
			carry = 1;
			answer = answer - 10;
		}
		else{
			carry = 0;
		}
		
		start->insertF(answer);
		ptr1 = ptr1->backward;
		ptr2 = ptr2->backward;
	}

	while(ptr1 != NULL){
		answer = ptr1->stuff;

		if(carry == 1){
			answer++;
			carry = 0;
			if(answer >= 10){
				carry = 1;
				answer = answer - 10;
			}
		}
		start->insertF(answer);
		ptr1 = ptr1->backward;
	}

	while(ptr2 != NULL){
		answer = ptr2->stuff;
		if(carry == 1){
			answer++;
			carry = 0;
			if(answer >= 10){
				carry = 1;
				answer = answer - 10;
			}
		}
		start->insertF(answer);
		ptr1 = ptr1->backward;
	}

	if(carry == 1){
		start->insertF(1);
	}

	return (*start);
}


As far as I can tell, it has something to do with the return value for the overloading function? But I am not sure how I would change that...

Thanks for the help!

Note: This is not the entire code, I just provided the part where the error is popping up.
You declared operator + to return a LongInt and defined it to return LongInt&. Those are very different things, so C++ can't decide which one you want.
I can guess that you are using a MS VC++ compiler. It is a bug of this compiler! It shall not see the friend function name until it will not be defined. So your code shall be compiled without an error even if the declaration of the friend function does not correspond to the definition of the function with the same name. The friend function and the definition of the other function with the same name are two different functions but the name of the friend function is not visible in the declaration region where the second function is defined.
But nevertheless next time you shall be careful and provide that the declaration of a function corresponds to its definition.
Last edited on
Topic archived. No new replies allowed.