Operator Overloading of >> in Classes causing issue

I am trying to make a class for Polynomial which will take input of the max degree of the polynomial and then accordingly, will set the maximum array size and will take inputs for all the powers.
I made a test program and overloaded the operator, however, I am getting two errors,

First,
"illegal reference to non-static member 'Polynomial::size' (Line 21, Line 22, Line 25, Line 27)"
Second,
"'Polynomial::size': non-standard syntax; use '&' to create a pointer to member (Line 25, Line 27)"

Now, what I understand by the first error is this that I have to make int size static.. But I don't want to that as every object of Polynomial will have a different size, so how can I tackle this issue?
And I don't understand what is it the second error is saying? :/

Can anyone help resolving these issues? Thank you!
Here is my test code for now,

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
#include <iostream>
using namespace std;
class Polynomial
{
private:
	int size; 
	float *po, *in; 
public:
	Polynomial()
	{
		po = NULL;
		in = NULL;
		size = 0;
	}
	friend istream &operator >> (istream &inp, Polynomial &r)
	{
		cout << "Enter Max power variable: "; 
		int max; 
		inp >> max; 
		r.size = max + 1;
		r.po = new float[size];
		r.in = new float[size];
		for (int i = 0; i < r.size; i++)
		{
			cout << "Enter Co-efficient for power(" << size - 1 << "): "; 
			inp >> r.in[i];
			r.po[size - (i + 1)] = size - 1;
		}
	}
	friend ostream &operator << (ostream &out, Polynomial &r)
	{
		for (int i = 0; i < r.size; i++)
		{
			out << r.in[i];
			if (i == 0)
				continue; 
			if (i == 1)
				cout << "x";
			else
				cout << "x^" << r.po[i];
		}
	}
};
int main()
{
	Polynomial r; 
	cout << endl << endl; 
	cout << r; 
	cout << endl;
	system("pause");
	return 0;
}
Last edited on
friend functions are called friends because the functions themselves shouldn't be defined within the class, but the function signature should be declared as being a friend of the class. Edit: This is not true. Ignore this.

Your class should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Polynomial {
    // ...
	friend istream &operator >> (istream &inp, Polynomial &r);
	friend ostream &operator << (ostream &out, Polynomial &r);
 
};

// *outside of class*

istream &operator >> (istream &inp, Polynomial &r)
{
	// ...
	return inp;
}
friend ostream &operator << (ostream &out, Polynomial &r)
{
	// ...
	return out;
}


With the functions themselves being defined outside of the class.

Your functions also need to return the istream or ostream object. And your ostream << operator should be internally using out instead of cout.
Last edited on
Now, what I understand by the first error is this that I have to make int size static.. But I don't want to that as every object of Polynomial will have a different size, so how can I tackle this issue?

Access size from r the same way you access po and in.

 
r.po = new float[r.size];


friend functions are called friends because the functions themselves shouldn't be defined within the class

It is possible to define friend functions inside the class definition. It won't make them member-functions.
Ah, my bad. Guess the issue was just the other stuff you mentioned.
Topic archived. No new replies allowed.