operator overloading problem

Pages: 12
i know its a big thing to look at
whats wrong with my assignment or addition operator
and here is the promblem

In function 'int main()':|

 no match for 'operator=' in 'r = Polynomial<T>::operator+(Polynomial<T>&) [with T = int](((Polynomial<int>&)(& q)))'|

note: candidates are: Polynomial<T> Polynomial<T>::operator=(Polynomial<T>&) [with T = int]|




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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include<iostream>
#include<fstream>
#include<sstream>
#include<math.h>
#include<string>
using namespace std;
template<class T>
class Polynomial; //forward declaration
template<class T>
istream& operator>>(istream& in,Polynomial<T>& obj);//forward declaration
template<class T>
ostream& operator <<(ostream& out,const Polynomial<T>& obj);
//---------------------------------------------------------------------------
//class of Term that has two variables of type T

//--------------------------------------------------------
//class of polynomial that will contain a variable of term
template<class T>
class Polynomial
{
    private:
        Term<T>* arrOfTerm;
    public:
        Polynomial<T>();//will intialize a polynomial form coeffi 1 and exp1
        Polynomial(const Polynomial& obj);//copy constructor
        Polynomial(T coeffient[],int order);//coeffi are in array and order given
        Polynomial(Term<T> a[],int size);//the poly will be init by an array of
        //term which contains coeffi and exp and size is the size of array of term
        Polynomial(string);//a poly will be given in string form
        ~Polynomial();

        // String, UnString
		string toString();
		void pasrePolynomial(string);

        //methods
        void setXthTerm(int x);//done
        void showXthTerm(int x);//done
        T evaluate();//done

        //input and display functions
        void getInput();//done
        static Polynomial input();//done
        void display();//done


        //operators  =, +, -, *, ==, !=, <<, >>, []
        Polynomial operator =( Polynomial<T>& obj);//done
        //static Polynomial<T> operator =(const Polynomial<T>& obj);
        Polynomial operator +( Polynomial& obj);
        //static Polynomial operator +(const Polynomial& obj1,const Polynomial& obj2);
        Polynomial operator -(const Polynomial& obj);
        Polynomial operator *(const Polynomial& obj);
        bool operator ==(const Polynomial& obj);//done
        bool operator !=(const Polynomial& obj);//done
        friend istream& operator >><T>(istream& in,Polynomial<T>& obj);//done
        friend ostream& operator <<<T>(ostream& out,const Polynomial<T>& obj);//done
        void operator [](int index);//done
};
template<class T>
Polynomial<T>::Polynomial()
{
    arrOfTerm = new Term<T>[3];
}
template<class T>
Polynomial<T>::Polynomial(const Polynomial& obj)
{
    int size;
    size = obj.arrOfTerm[0].getIndex();
    delete []arrOfTerm;
    arrOfTerm = new Term<T>[size];
    for(int i =0; i < size;i++)
    {
        arrOfTerm[i].setCoeffi( obj.arrOfTerm[i].getCoeffi());
        arrOfTerm[i].setExp(obj.arrOfTerm[i].getExp());
        arrOfTerm[i].setIndex(i+1);
    }
}
template<class T>
Polynomial<T>::Polynomial(T coefficient [], int order)
{
    delete []arrOfTerm;
    int pow = order;
    arrOfTerm = new Term<T>[order];
    for(int i = 0 ; i < order; i++,pow--)
    {
        arrOfTerm[i].setCoeffi( coefficient[i]);
        arrOfTerm[i].setExp(pow);
        arrOfTerm[i].setIndex(order);
    }
}
template<class T>
Polynomial<T>::~Polynomial()
{
    delete []arrOfTerm;
}

//input and display functions
template<class T>
Polynomial<T> Polynomial<T>::input()
{
    Polynomial obj;
    obj.getInput();
    return obj;
}
template<class T>
void Polynomial<T>::getInput()
{
    delete []arrOfTerm;
    T coefficient;
    T exponent;
    int noOfTerms;
    cout << "Enter how many terms you want to enter : ";
    cin >> noOfTerms;
    arrOfTerm = new Term<T>[noOfTerms];
    for(int i =0 ; i < noOfTerms;i++)
    {
        cout <<"Enter the coefficient of term  " <<i << "  : ";
        cin >>coefficient;
        cout <<"Enter  the  exponenet of term  " <<i << "  : ";
        cin >>exponent;
        arrOfTerm[i].setExp(exponent);
        arrOfTerm[i].setCoeffi(coefficient);
        arrOfTerm[i].setIndex(noOfTerms);
    }
}
template<class T>
void Polynomial<T>::display()
{
    for(int i =0 ; i < arrOfTerm[0].getIndex() ; i++)
    {
        cout << arrOfTerm[i].getCoeffi() << "x^"<<arrOfTerm[i].getExp();
        if(i < arrOfTerm[0].getIndex()-1)
        {
            cout << " + ";
        }
    }
}
//operators
template<class T>
ostream& operator<<(ostream& out,Polynomial<T>& obj)
{
    obj.display();
    return out;
}
template<class T>
istream& operator>>(istream& in , Polynomial<T>& obj)
{
    obj.getInput();
    return in;
}
template<class T>
Polynomial<T> Polynomial<T>::operator =( Polynomial<T>& obj)
{
    int size;
    size = obj.arrOfTerm[0].getIndex();
    delete []arrOfTerm;
    arrOfTerm = new Term<T>[size];
    for(int i =0; i < size;i++)
    {
        arrOfTerm[i].setCoeffi( obj.arrOfTerm[i].getCoeffi());
        arrOfTerm[i].setExp(obj.arrOfTerm[i].getExp());
        arrOfTerm[i].setIndex(size);
    }
}
template<class T>
bool Polynomial<T>::operator ==(const Polynomial& obj)
{
    if(arrOfTerm[0].getIndex()==obj.arrOfTerm[0].getIndex())
    {
        for(int i =0  ; i < arrOfTerm[0].getIndex();i++)
        {
            if(obj.arrOfTerm[i].getCoeffi()!=arrOfTerm[i].getCoeffi()
               || obj.arrOfTerm[i].getExp()!=arrOfTerm[i].getExp())
            {
                return 0;
            }
        }
    }
    return 1;
}
template<class T>
bool Polynomial<T>::operator !=(const Polynomial& obj)
{
    if(arrOfTerm[0].getIndex()==obj.arrOfTerm[0].getIndex())
    {
        for(int i =0  ; i < arrOfTerm[0].getIndex();i++)
        {
            if(obj.arrOfTerm[i].getCoeffi()!=arrOfTerm[i].getCoeffi()
               || obj.arrOfTerm[i].getExp()!=arrOfTerm[i].getExp())
            {
                return 1;
            }
        }
    }
    return 0;
}
template<class T>
void Polynomial<T>::operator[](int i)
{
    this->showXthTerm(i);
}
template<class T>
Polynomial<T> Polynomial<T>::operator+( Polynomial<T>& obj)
{
    Polynomial a;
    delete []a.arrOfTerm;
    int newSize;
    int noOfSimilarTerms;
    for(int i =0 ;  i< arrOfTerm[0].getIndex();i++)
    {
        for(int j =0; j<obj.arrOfTerm[0].getIndex();j++)
        {
            if(arrOfTerm[i].getExp() == obj.arrOfTerm[j].getExp())
            {
                noOfSimilarTerms++;
            }
        }
    }
    newSize = (obj.arrOfTerm[0].getIndex() + arrOfTerm[0].getIndex()) - noOfSimilarTerms;
    a.arrOfTerm = new Term<T>[newSize];
    for(int i =0 ;  i< arrOfTerm[0].getIndex();i++)
    {
        for(int j =0; j<obj.arrOfTerm[0].getIndex();j++)
        {
            if(arrOfTerm[i].getExp() == obj.arrOfTerm[j].getExp())
            {
               a.arrOfTerm[i].setCoeffi(arrOfTerm[i].getCoeffi()+obj.arrOfTerm[j].getCoeffi());
            }
        }
    }
}
int main ()
{

    //checking addition operator
    int a[3] ={2,2,2};
    Polynomial<int> p(a,3);
    Polynomial<int> q(a,3);
    Polynomial<int> r(a,3);
    //cin >>p;
    //cin >>q;
    r = p+q;
    cout << r;
    return 0;
}
Last edited on
few remarks, if you don't mind

1. your operator+ is not returning anything
2. check the correct way to write operator= (which is not returning anything)
3. check const-correctness
4. all those delete's seem dangerous :)
Last edited on
Templated classes must be defined inline.
@L B

It's fine like this. Why do you say so?
Templated classes must be defined inline.

please elobrate .. not getting what you want to say .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Polynomial<T> Polynomial<T>::operator =( Polynomial<T>& obj)
{
    int size;
    size = obj.arrOfTerm[0].getIndex();
    delete []arrOfTerm;
    arrOfTerm = new Term<T>[size];
    for(int i =0; i < size;i++)
    {
        arrOfTerm[i].setCoeffi( obj.arrOfTerm[i].getCoeffi());
        arrOfTerm[i].setExp(obj.arrOfTerm[i].getExp());
        arrOfTerm[i].setIndex(size);
    }
    return *this;
}


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
Polynomial<T> Polynomial<T>::operator+( Polynomial<T>& obj)
{
    Polynomial a;
    delete []a.arrOfTerm;
    int newSize;
    int noOfSimilarTerms;
    for(int i =0 ;  i< arrOfTerm[0].getIndex();i++)
    {
        for(int j =0; j<obj.arrOfTerm[0].getIndex();j++)
        {
            if(arrOfTerm[i].getExp() == obj.arrOfTerm[j].getExp())
            {
                noOfSimilarTerms++;
            }
        }
    }
    newSize = (obj.arrOfTerm[0].getIndex() + arrOfTerm[0].getIndex()) - noOfSimilarTerms;
    a.arrOfTerm = new Term<T>[newSize];
    a.arrOfTerm[0].setIndex(newSize);
    for(int i =0 ;  i< a.arrOfTerm[0].getIndex();i++)
    {
        for(int j =0; j<obj.arrOfTerm[0].getIndex();j++)
        {
            if(arrOfTerm[i].getExp() == obj.arrOfTerm[j].getExp())
            {
               a.arrOfTerm[i].setCoeffi(arrOfTerm[i].getCoeffi()+obj.arrOfTerm[j].getCoeffi());
            }
        }
        a.arrOfTerm[i].setCoeffi()
    }
    return a;
}


here are the functions

i know that add is not adding properly but still the same error msg

ummm const correctness??
Templated classes have to be defined inline because of the way templates work. Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
class SomeClass
{
    SomeClass()
    {
        //You define the member functions, constructors, etc. INSIDE the class
    }

    void DoSomething(int x)
    {
        //
    }
};
can we focus on the problem at hand ..... I am nearing submission deadline :(
Thanks L B :)
@LB: You mean in the same file.

Polynomial& operator=(const Polynomial &); Because you can't take a reference from a temporary (that is what operator+ returns). That will make it compile. I could not tested as you didn't provide the Term class.

You are exposing to danger with the use of pointers. You could avoid that with std::valarray or std::vector
its a runtime error this time
please help sent entire night on this and this one last thing isnot working


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
template<class T>
class Term
{
    private:
        T coeffi;
        T exp;
        int index; //for storing the max index
    public:
        //setters and getters
        void setCoeffi(T);
        void setExp(T);
        T getCoeffi();
        T getExp();
        void setIndex(int);
        int getIndex();
        //constructors and distructors
        Term();//term with coeffi and exp  1
        //Term(int);//term with coeffi
        //Term(int, int);
        //friend istream& operator>>(istream& in,Term<T>&obj);
};

template<class T>
Term<T>::Term()
{
    coeffi =1;
    exp =1;
    index =1;
}
template<class T>
void Term<T>::setIndex(int a)
{
    index =a;
}
template<class T>
int Term<T>::getIndex()
{
    return index;
}
template<class T>
void Term<T>::setCoeffi(T a)
{
    coeffi =a;
}
template<class T>
void Term<T>::setExp(T a)
{
    if(a > 0)
    {
        exp = a;
    }
    else
    {
        throw -1;
    }

}
template<class T>
T Term<T>::getCoeffi()
{
    return coeffi;
}
template<class T>
T Term<T>::getExp()
{
    return exp;
}
yes i have tried with the &
As L B stated earlier you need to put the declaration and definition of the templated functions inline in the class not defined outside as you have it. (Unless I read his comments wrong)
closed account (1yR4jE8b)
No you don't. They just have to be visible by the compiler at compile-time, not link-time. This means they can be defined in the separately in the same header file, inline, or in a separate file which is #include'd by the header file.
Yes, darkestfright is right, inlining is just simpler :p
my problem ... my problem
Look at what are you doing
1
2
3
4
template<class T>
Polynomial<T>::Polynomial(T coefficient [], int order)
{
    delete []arrOfTerm; //¿when was arrOfTerm set to anything? 


Also, keep in mind the purpose of the forum. Don't worry, it will not derail
Last edited on
NOT HELPING


runtime error
Fine, we leave you alone then.
like you were ever helping...
Pages: 12