ここでやろうとしているのは、% 演算子をオーバーロードして、分子に特定の数値を掛けることです。プログラム内の他のオーバーロードされた演算子はすべて完全に機能しますが、これは問題を引き起こしています。この問題を分離するために、このオーバーロードされた演算子のみを使用して別の小さなプログラムを作成しました。
modu.h ファイルから (いくつかの無関係な部分が省略されていることを願っています):
class fraction {
private:
int numerator, denominator;
public:
fraction ();
fraction (int n, int d);
void print ();
void operator% (int x);
};
// the constructors and print functions work fine for the other overloaded
// operators so I decided to omit them
void fraction::operator%(int x) {
numerator = numerator * x;
}
このようにmain.cppファイルでそれらを使用すると
fraction frct (2, 3); // declare a fraction with a value of 2/3
frct = frct % 3;
これらのエラーが発生します
modu.cpp:9:17: error: no match for ‘operator=’ in ‘frct = frct.fraction::operator%(3)’
modu.cpp:9:17: note: candidate is:
In file included from modu.cpp:3:0:
modu.h:6:7: note: fraction& fraction::operator=(const fraction&)
modu.h:6:7: note: no known conversion for argument 1 from ‘void’ to ‘const fraction&’
そして、「 frct = frct.%(3); 」のように使用すると、エラーが発生します。
modu.cpp:9:15: error: expected unqualified-id before ‘%’ token
セミコロンと中かっこの欠落を何度か確認しましたが、すべてが正常に機能しているように見えます。