Operator Overloading

Hello everyone,

This was my homework so it is not anymore I'm just curious to solve it. Here is what this program was suppose to do. Main program was given to me by instructor. He wanted us to write a code that makes this main work. He wanted us to overload operators in order to make addition, subtraction etc... But somehow I failed whatever I do. Can you guys correct me where I'm doing wrong and what, I looked google for this unfortunately I couldn't understand. Thank you for your helps !

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
//////////// Monomial.h////////////
#ifndef MONOMIAL_H
#define MONOMIAL_H
#include <iostream>
#endif

using namespace std;

struct MonomialPtr{
	int coef;
	int exp;
};

class Monomial{
public:
	Monomial(int); // Constructor for 1 input that is coefficient
	Monomial (int, int); // C'tor that 1st is coefficient and 2nd is exponent
	const Monomial &operator += (const MonomialPtr & ct);
	const Monomial &operator + (const MonomialPtr &num);
        friend ostream &operator << (ostream & os, const MonomialPtr & ct); /* Friend function for I/O operator overload*/

private:
	MonomialPtr *ptr;

};

const  Monomial &Monomial::operator += (const MonomialPtr & mon){
    MonomialPtr *result = ptr;
	result->coef += mon.coef;
	return result->coef;

}

const Monomial &Monomial::operator+(const MonomialPtr &num){

	return ptr->coef += num.coef; // warning C4172: returning address of local variable or temporary

}

ostream &operator << (ostream & output, const MonomialPtr & mon){
	
	output<<mon.coef<<"*"<<"X"<<"^"<<mon.exp<<endl;
	return output;

}
////////////////// Monomial.cpp//////////////
#include"Monomial.h"
#include <iostream>
#include <sstream>

using namespace std;
Monomial::Monomial(int coef){
	MonomialPtr *ptr = new MonomialPtr;
	ptr->coef = coef;
	ptr->exp = 0;
}

Monomial::Monomial(int coef, int exp){

	MonomialPtr *ptr = new MonomialPtr;
	ptr->coef = coef;
	ptr->exp = exp;
}


////////////////// Main.cpp//////////////////
#include <iostream>
using namespace std;
#include "Monomial.h"
#include "Polynomial.h"

int main () {
Monomial m1(1);
Monomial m2(2);
cout << m1+=m2; // Says no operator matches
Monomial m3(3);
Monomial m11(1,1);
Monomial m21(2,1);
Monomial m31(3,1);
Monomial m12(1,2);
Monomial m22(2,2);
Monomial m32(3,2);
cout << "m1+3:" << m1+3 << endl; // Again no operator matches..
cout << "m1+m2:" << m1+m2 << endl;
cout << "m1+m21:" << m1+m21 << endl;
cout << "m1 - m21:" << m1 - m21 << endl; // "-" Not implemented yet.
cout << "m11+m21+m31:" << m11+m21+m31 << endl;
cout << "m12+m22+m32:" << m12+m22+m32 << endl;

cout << "m1*3:" << m1*3 << endl; // "*" Not implemented yet.
cout << "3*m1:" << 3*m1 << endl;
cout << "m1*m2:" << m1*m2 << endl;
cout << "m1*m21:" << m1*m21 << endl;
cout << "m11*m21*m31:" << m11*m21*m31 << endl;
cout << "m12*m22*m32:" << m12*m22*m32 << endl;
Monomial m = m21;
m += m31;
m *= m32;
cout << "m:" << m << endl;
cout << "m(5):" << m(5) << endl;

return 0;
}
Last edited on
closed account (Dy7SLyTq)
you made << private
you made << private
can you explain it please ?
closed account (Dy7SLyTq)
just put line 15 underneath the public: line
it still underlines << at line 76
Did you try putting parentheses around m1+=m2?
Did you try putting parentheses around m1+=m2?
Yes now it doesn't underline it thank you. I still have problems with + operator and += operator what am I doing wrong with them ?
closed account (Dy7SLyTq)
could it be that << has higher precedence than +/+=?
When it comes to compound assignment operators (+=, -=, *=, /=, etc.), the convention is to allow the function to modify the object (*this) and return a reference to the same object.
Your current operator+= function does not modify the object and returns a new, temporary object.

Your operator+ function attempts to return a constant reference to a temporary object that may or may not exist anymore.

Here are the basic semantics of binary arithmetic operators:
1
2
3
4
5
6
7
   //Notice how we will return a non-const reference
Monomial& Monomial::operator+=(const Monomial&);
   //Note how this time we will return a copy/value
   //   In addition, binary functions are usually non-member functions
   //   If you do make it a member function, at least mark it as read-only
   //   since it is not supposed to modify its parameters.
Monomial operator+(const Monomial&, const Monomial&);


And their definitions based on your code would look similar to:
1
2
3
4
5
6
7
8
Monomial& Monomial::operator+=(const Monomial& rhs){
   ptr->coef += mon.coef; //Modify this object
   return *this; //Now return this object to allow for "chaining", e.g.
           // (m1+=m2) += m2;
}
Monomial operator+(const Monomial& lhs, const Monomial& rhs){
   return Monomial(lhs) += rhs; //Basically like your original definition
}


Edit: Did not see DTSCode's response.
I think so. I have that problem all the time when I write something like:
cout << FOO + BAR;
Last edited on
http://en.cppreference.com/w/cpp/language/operator_precedence

<< has higher precedence than += but lower than +

So cout << FOO + BAR; has no problem. cout << FOO += BAR; does
closed account (Dy7SLyTq)
so then it would try to print foo and then call null += BAR
More like
cout += BAR
.
When it comes to compound assignment operators (+=, -=, *=, /=, etc.), the convention is to allow the function to modify the object (*this) and return a reference to the same object.
Your current operator+= function does not modify the object and returns a new, temporary object.

Your operator+ function attempts to return a constant reference to a temporary object that may or may not exist anymore.

Thank you ! But when I write two parameters to + operator function it underlines it and says that there are too many parameters for that operator.


Edit:

When I just tried to compile just the header with emty main it gave me 4 errors:

1>Monomial.obj : error LNK2005: "public: class Monomial & __thiscall Monomial::operator+=(struct MonomialPtr const &)" (??YMonomial@@QAEAAV0@ABUMonomialPtr@@@Z) already defined in HW5_Main.obj

1>Monomial.obj : error LNK2005: "public: class Monomial & __thiscall Monomial::operator+(struct MonomialPtr const &)" (??HMonomial@@QAEAAV0@ABUMonomialPtr@@@Z) already defined in HW5_Main.obj

1>Monomial.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct MonomialPtr const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABUMonomialPtr@@@Z) already defined in HW5_Main.obj

1>C:\Users\Kaan\documents\visual studio 2010\Projects\HW5\Debug\HW5.exe : fatal error LNK1169: one or more multiply defined symbols found
Last edited on
My problem is now I can't do multiple operators at once like m11+m21+m31, it should be 6*x but the result that program printed was 4*x how can I solve this. Also it is same when I multiply more than 2 variables. Thank you !

Edit: Code modified.
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
////header/////
#ifndef MONOMIAL_H
#define MONOMIAL_H
#include <iostream>
#endif

using namespace std;

class Monomial{
public:
	Monomial(int);
	Monomial (int, int);
	Monomial &operator += (const Monomial & ct);
	Monomial &operator+(const Monomial&);
	Monomial &operator-(const Monomial&);
	Monomial &operator+(const int &num);
	Monomial &operator*(const int &num);
	Monomial &operator*(const Monomial &num);
	friend ostream &operator << (ostream & os, const Monomial & ct);


private:
	int coef;
	int exp;
	int result;
	int resultExp;
};

ostream &operator << (ostream & os, const Monomial & ct);
////// monomial.cpp/////
#include"Monomial.h"
#include <sstream>

using namespace std;
Monomial::Monomial(int c){
	coef = c;
	exp = 0;
	result = 0;
	resultExp = 0;
}

Monomial::Monomial(int c, int numExp){

	coef = c;
	exp = numExp;
	result = 0;
	resultExp = 0;

}

Monomial &Monomial::operator += (const Monomial & mon){
	if(this->exp == mon.exp){
		this->result = 0;
		this->result = coef + mon.coef;
		return *this;
	}
	cout << "Error: Degrees do not match:0 1 " << endl;
}

Monomial &Monomial::operator+(const int &num){
	this->result = 0;
	this->result = this->coef + num;
	this->resultExp = this->exp;
	return *this;

}

Monomial &Monomial::operator+(const Monomial &num){
	if(this->exp == num.exp){
		this->result = this->coef + num.coef;
		this->resultExp = this->exp;
		return *this;
	}
	else 
		cout << "Error: Degrees do not match" << endl;

}

Monomial &Monomial::operator-(const Monomial &num){
	if(this->exp == num.exp){
		this->result = 0;
		this->result = this->coef - num.coef;
		return *this;
	}
	cout << "Error: Degrees do not match" << endl;
}

Monomial &Monomial::operator*(const int &num){
	this->result = 0;
	this->result = this->coef * num;
	this->resultExp = this->exp;
	return *this;
}

Monomial &Monomial::operator*(const Monomial &num){
	this->result = 0;
	this->resultExp = 0;
	this->result = this->coef * num.coef;
	this->resultExp = this->exp + num.exp;
	return *this;
}

ostream &operator << (ostream & output, const Monomial & mon){
	output<<mon.result;
	if(mon.resultExp) output << "*X^"<<mon.resultExp<<endl;

	return output;

}
//// main /////
int main () {
Monomial m1(1);
Monomial m2(2);
Monomial m3(3);
Monomial m11(1,1);
Monomial m21(2,1);
Monomial m31(3,1);
Monomial m12(1,2);
Monomial m22(2,2);
Monomial m32(3,2);
cout << "m1+3:" << m1+3 << endl;
cout << "m1+m2:" << m1+m2 << endl;
cout << "m1+m21:" << m1+m21 << endl;
cout << "m1 - m21:" << m1 - m21 << endl;
cout << "m11+m21+m31:" << (m11+m21)+m31 << endl;
cout << "m12+m22+m32:" << m12+m22+m32 << endl;

cout << "m1*3:" << m1*3 << endl;
//cout << "3*m1:" << 3*m1 << endl;
cout << "m1*m2:" << m1*m2 << endl;
cout << "m1*m21:" << m1*m21 << endl;
cout << "m11*m21*m31:" << m11*m21*m31 << endl;
cout << "m12*m22*m32:" << m12*m22*m32 << endl;
Monomial m = m21;
m += m31;

return 0;
}
Last edited on
closed account (Dy7SLyTq)
unforunately you need a bunch of parenthenses
I can't do multiple operators at once like m11+m21+m31, it should be 6*x but the result that program printed was 4*x how can I solve this

Your new program does not modify the coefficient or the exponent when it performs addition (instead it modifies some other member variables, which shouldn't have been added in the first place, but which you print with operator<<). So the result you're seeing is the result of the last addition in the expression.
Last edited on
unforunately you need a bunch of parenthenses


Thanks for your reply, but because it was a homework, modifying the main was forbidden.


Your new program does not modify the coefficient or the exponent when it performs addition (instead it modifies some other member variables, which shouldn't have been added in the first place, but which you print with operator<<). So the result you're seeing is the result of the last addition in the expression.


Thank you for your reply, but when I modify them it calulates other operations wrong like:

m1+m2 = 3
when trying to use m1 again this time
m1+m3 = 6 <- it gives this result

thats why I used another member variable. But as you say now I can not completa multiple aritmatic operations successfully. Any suggestions ?
Last edited on
Topic archived. No new replies allowed.