Search code examples
c++inheritanceoperator-overloadingoverloadingparent

Operation on Operator= of a child class with a parent class


I am working on an assignment creating a polynomial calculator which can perform addition, subtraction, multiplication and division. When i used the main.cpp provided to test the operator= function, the compiler keep saying: "no operator found which takes a right-hand operand of type 'Polynomial'.

Here is the error i receive:

main.cpp(61): error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'Polynomial' (or there is no acceptable conversion)
          c:\c++ project\comp2012_assignment_3\comp2012_assignment_3\IntegerPolynomial.h(44):
          could be 'IntegerPolynomial &IntegerPolynomial::operator =(const IntegerPolynomial &)'
          while trying to match the argument list '(IntegerPolynomial, Polynomial)'

below are part of my code:

main.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <string>
#include "Polynomial.h"
#include "IntegerPolynomial.h"

using namespace std;

template <typename T>
T readPoly(const char* filename) {
    ifstream fin(filename, ifstream::in);
    T temp;
    fin >> temp;
    fin.close();
    return (temp); 
}
int main(void)
{
    Polynomial a = readPoly<Polynomial>("input1.txt");
    Polynomial b = readPoly<Polynomial>("input2.txt");
    Polynomial d;

    b.sort();
    cout << "sorted b=" << b << endl;

    IntegerPolynomial ia = readPoly<IntegerPolynomial>("input1.txt");
    IntegerPolynomial ib = readPoly<IntegerPolynomial>("input2.txt");

    int iarry[5] = {1, 3, 5, 9, 10};
    IntegerPolynomial id(iarry, 5);

    cout << "ia=" << ia <<endl;
    cout << "ib=" << ib <<endl;
    cout << "id=" << id <<endl;

    d=b;
    cout << "d=b=" << d << endl;
    d=ib;
    cout << "d=ib=" << d << endl;
    id=b;
    cout << "id=b=" << id << endl;
    d=a-b;

    return 0;
}

polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <list>
#include <cstdlib>
#include <cmath>
using namespace std;

class Polynomial
{
  public:
    Polynomial& operator=(const Polynomial& a);
  protected:
    std::list<Term> polyList;
};
#endif

polynomial.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>
#include <list>
#include "Polynomial.h"
// Assignment operator
Polynomial& Polynomial::operator=(const Polynomial& a) {
    polyList.clear();
    for (std::list<Term>::const_iterator a_iterator = a.polyList.begin(), end = a.polyList.end(); a_iterator != end; ++a_iterator) {
        const Term curr_Term = *a_iterator;
        polyList.push_back(curr_Term);
    }
    return *this;
}

integerpolynomial.h

#ifndef INTEGERPOLYNOMIAL_H
#define INTEGERPOLYNOMIAL_H

#include <iostream>
#include "Polynomial.h"
using namespace std;
class IntegerPolynomial : public Polynomial{
  public:
    // Default constructor 
    IntegerPolynomial();
);
#endif

integerpolynomial.cpp

#include "IntegerPolynomial.h"

using namespace std;

// Default constructor 
IntegerPolynomial::IntegerPolynomial() {
}
// print the polynomial a in decreasing order of exponent
void IntegerPolynomial::print(ostream& os) const {
    os << toString();
}

note: i tried to only include the code section which is related to the problem as the whole file of my code is too long.

what actually is the problem since in the main.cpp

d=b;
cout << "d=b=" << d << endl;
d=ib;
cout << "d=ib=" << d << endl;

works fine.

problem arise for this line.

id=b;

Is there any problem associated with the definition of the function and what is the solution. Thanks a lot.


Solution

  • The problem is that assignment operators are not really inherited - if a class does not declare an assignment operator, the compiler declares one implicitly (see [over.ass]). The implicitly declared copy assignment operator for class X has signature X& operator= (const X&).

    So your code id = b invokes the (implicitly declared) assignment operator for IntegerPolynomial, which expects another IntegerPolynomial on the right-hand side.

    If you need to assign arbitrary Polynomials into IntegerPolynomials, you'll have to define such an assignment operator explicitly. I think you'd have to do this anyway: how would you generically assign a general polynomial to an integral one?