在Operator上操作=具有父类的子类

时间:2012-12-05 09:52:53

标签: c++ inheritance operator-overloading overloading parent

我正在创建一个多项式计算器,可以执行加法,减法,乘法和除法。当我使用提供的main.cpp来测试operator =函数时,编译器一直说:“没有找到哪个操作符采用了'Polynomial'类型的右手操作数。

以下是我收到的错误:

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)'
下面的

是我的代码的一部分:

的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();
}

注意:我试图只包含与问题相关的代码部分,因为我的代码的整个文件太长了。

因为在main.cpp

中实际上是什么问题
d=b;
cout << "d=b=" << d << endl;
d=ib;
cout << "d=ib=" << d << endl;

工作正常。

这条线出现了问题。

id=b;

是否存在与功能定义相关的问题以及解决方案是什么。非常感谢。

2 个答案:

答案 0 :(得分:0)

您的Polynomial::operator=是私密的,应该是:

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

答案 1 :(得分:0)

问题是赋值运算符并不是真正继承的 - 如果一个类没有声明赋值运算符,编译器会隐式声明一个(参见[over.ass])。类X的隐式声明的复制赋值运算符具有签名X& operator= (const X&)

所以你的代码id = b调用IntegerPolynomial的(隐式声明的)赋值运算符,它在右边需要另一个IntegerPolynomial

如果您需要将Polynomial分配给IntegerPolynomial,则必须明确定义此类赋值运算符。我认为你无论如何都必须这样做:你如何将一般多项式一般地分配给一个整数?