我正在做一个创建多项式计算器的作业,该计算器可以执行加法、减法、乘法和除法。当我使用提供的 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;
是否存在与函数定义相关的问题以及解决方案是什么。非常感谢。