【问题标题】:Operation on Operator= of a child class with a parent class对带有父类的子类的 Operator= 的操作
【发布时间】:2012-12-05 09:52:52
【问题描述】:

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

这是我收到的错误:

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;
}

多项式.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

多项式.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;
}

整数多项式.h

#ifndef INTEGERPOLYNOMIAL_H
#define INTEGERPOLYNOMIAL_H

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

整数多项式.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;

函数的定义有什么问题吗?解决方法是什么。非常感谢。

【问题讨论】:

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


    【解决方案1】:

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

    因此,您的代码 id = bIntegerPolynomial 调用(隐式声明的)赋值运算符,它期望右侧有另一个 IntegerPolynomial

    如果您需要将任意Polynomials 分配给IntegerPolynomials,则必须明确定义这样的赋值运算符。我认为无论如何您都必须这样做:您如何将一般多项式分配给积分多项式?

    【讨论】:

    • 这与我一直在想的很接近,但是这种情况的解决方案是什么?我应该用 IntergerPolynomial 重载带有参数列表的函数吗?
    【解决方案2】:

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

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

    【讨论】:

    • 对不起,我已经公开了。我忘了复制“公众:”。问题依然存在。
    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多