Using two overloaded operators in same line but compiler not allowing it, why?

Ok, so I made a Polynomial class and I overloaded arithmetic and other operators for that class..
However, when I used two overloaded operators in the same line, it generates an error...

Here is the code for my header file of 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
#pragma once
#include <iostream>
using namespace std; 
class Polynomial
{
	int deg; 
	int *in;
	int *po;
	int size; 
public:
	Polynomial()
	{
		deg = 0;
		in = NULL;
		po = NULL;
		size = 0;
	}
	friend istream &operator>>(istream &inp, Polynomial &p);
	friend ostream &operator <<(ostream &out, Polynomial &p);
	Polynomial operator+(Polynomial &p);
	Polynomial operator-(Polynomial &p);
	Polynomial operator*(Polynomial &p);
	Polynomial operator/(Polynomial &p);
	Polynomial& operator+=(Polynomial &p);
	Polynomial& operator-=(Polynomial &p);
	Polynomial& operator*=(Polynomial &p);
	void setinput(int, int); 
	int getinput(int);
};



Polynomial.cpp (only showing + operator overloading since that's enough I believe.. Don't want to make it too big a post)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Polynomial Polynomial:: operator+(Polynomial &p)
{
	if (this->deg != p.deg)
	{
		cout << "Degree of Polynomials don't match" << endl;
		return *this; 
	}
	for (int i = 0; i < this->size; i++)
	{
		this->in[i] = this->in[i] + p.in[i]; 
	}
	return *this;
	
}

(One more thing I want to point out. This is not a proper way for addition, however, this is just a demonstration)

Functions.cpp file (showing only ofstream operator)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ostream &operator <<(ostream &out, Polynomial &p)
{
	for (int i = 0; i < p.size; i++)
	{
		if (p.in >= 0)
			cout << "+"; 
		cout << p.in[i];
		if (p.po[i] == 0)
			;
		else if (p.po[i] == 1)
			cout << "x";
		else
		{
			cout << "x^";
			cout << p.po[i]; 
		}
	}
	return out; 
}


This is my main.cpp now,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "Polynomial.h"
#include "Functions.h"
using namespace std;
int main()
{
	Polynomial p, h; 
	cin >> p;
	cin >> h; 
	p + h; 
	cout << "Additon: " << p+h; 
	cout << "Subtraction: " << p - h;
	cout << endl;
	cout << p;
	system("pause");
	return 0;

}


Now, I am getting an error here... when I try to cout << p+h;
p+h executes correctly...
cout << p executes correctly as well.
However, I am getting these two errors for both of the lines where I try to add and do cout together,
Can anyone help to resolve this issue, please?
1
2
no operator "<<" matches these operands. 
Error	C2679	binary '<<': no operator found which takes a right-hand operand of type 'Polynomial' (or there is no acceptable conversion)	

Last edited on
The + operator does not modify its operands so you should mark the parameter and the function as const.

                                            Because you don't modify *this (left operand)
                                            |
Polynomial operator+(const Polynomial& p) const;
                       |
                       Because you don't modify p (right operand)

If you do the same (adding const where appropriate) with the other operators I think it should work. The reason you got an error is because a non-const reference (e.g. the second parameter of your operator<<) cannot bind to a temporary object (e.g. the return value of operator+).
Last edited on
Topic archived. No new replies allowed.