0

我正在做一个创建多项式计算器的作业,该计算器可以执行加法、减法、乘法和除法。当我使用提供的 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)'

以下是我的代码的一部分:

主文件

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

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

4

2 回答 2

0

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

class Polynomial
{
public:
    Polynomial& operator=(const Polynomial& a);
protected:
    std::list<Term> polyList;
};
于 2012-12-05T10:01:09.723 回答
0

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

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

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

于 2012-12-05T10:18:29.677 回答