Problem with member func. O.L

while Compiling this code, compiler(both dev c++ and visual studio), return following Errors :
Error 1 error C2801: 'operator []' must be a non-static member
Error 2 error C2673: 'operator []' : global functions do not have 'this'
Error 3 error C2227: left of '->size' must point to
Error 4 error C2065: 'poly' : undeclared identifier
Error 5 error C2228: left of '.get_p' must have class/struct/union

i can't really understand why number 1 and 2 occured! while non of them is true.

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
#include<iostream>
#include<conio.h>
using namespace std;
class term{
private:
	int c;
	int p;
public:
	term(int=0,int=0);
	void set_c(int);
	void set_p(int);
	int get_c();
	int get_p();
};
term::term(int a, int b){
	this->c=a;
	this->p=b;
}
void term::set_c(int a){
	this->c=a;
}
void term::set_p(int a){
	this->p=a;
}
int term::get_c(){
	return this->c;
}
int term::get_p(){
	return this->p;
}
//////////////////////////////////////////////////////
class polynomial{
private:
	term* poly;
	int size;
public:
	polynomial(int);
	int& operator[](int);
	polynomial operator+(polynomial);
	polynomial operator-(polynomial);
	polynomial operator*(polynomial);	
	int get_size();
	int set_size();
	int get_coef(int);
	void set_coef(int,int);
	~polynomial();
};
polynomial::polynomial(int s){
	poly=new term[s];
	this->size=s;
}
polynomial polynomial::operator +(polynomial poly2){
	int s,s2;
	polynomial result(3);
	if(this->size>poly2.get_size()){
		polynomial result(this->size);
		s2=poly2.get_size();
		}
	else {
		polynomial result(poly2.get_size());
		s2=this->get_size();
		}
	s=result.get_size();
	for(int i=0;i<=s;i++){
		for(int j=0;j<s2;j++)
			if(this->poly[i].get_p()==poly2[j])
				result.set_coef(i,this->poly[i].get_c()+poly2.get_coef(j));
	}
	return result;
}
int& operator[](int index){
	if(index<0 || index>this->size){
		cout<<"Error";
		exit(1);
	}
	return poly[index].get_p();
}
Last edited on
You forgot the polynomial:: in the operator[] definition. And also you're returning a reference to a temporary variable, since get_p() returns an int not an int&.
thank you very much! R0mai!
Topic archived. No new replies allowed.