0

私は、足し算、引き算、掛け算、割り算を実行できる多項式計算機を作成する割り当てに取り組んでいます。提供された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;

関数の定義に関連する問題はありますか、そして解決策は何ですか。どうもありがとう。

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は、の(暗黙的に宣言された)代入演算子を呼び出します。これは、右側にIntegerPolynomial別の代入演算子を期待します。IntegerPolynomial

Polynomial任意のsをsに割り当てる必要がある場合IntegerPolynomialは、そのような代入演算子を明示的に定義する必要があります。とにかくこれを行う必要があると思います。一般的な多項式を積分に一般的に割り当てるにはどうすればよいでしょうか。

于 2012-12-05T10:18:29.677 に答える