我在这里阅读了几篇关于这种错误的帖子,但我无法解决这个错误......很快我定义了运算符 int 和函数 f,无法编译。我测试了几件事,但无法解决问题....谢谢
ex1.cpp: In function ‘int main(int, char**)’:
ex1.cpp:35:13: error: ambiguous overload for ‘operator+’ in ‘a + 4’
ex1.cpp:35:13: note: candidates are:
ex1.cpp:35:13: note: operator+(int, int) <built-in>
In file included from ex1.cpp:3:0:
Fraccao.h:41:9: note: Fraccao operator+(const Fraccao&, const Fraccao&)
ex1.cpp:38:13: error: ambiguous overload for ‘operator+’ in ‘4 + a’
ex1.cpp:38:13: note: candidates are:
ex1.cpp:38:13: note: operator+(int, int) <built-in>
In file included from ex1.cpp:3:0:
Fraccao.h:41:9: note: Fraccao operator+(const Fraccao&, const Fraccao&)
班上:
class Fraccao {
int numerador;
int denominador;
public:
Fraccao(int num = 0, int deno = 1) : numerador(num), denominador(deno) {}
Fraccao & operator+=(const Fraccao &fra);
Fraccao & operator*=(const Fraccao &fra);
operator int() const;
const Fraccao & operator++();
const Fraccao operator++(int);
string getAsString() const;
};
Fraccao operator +(const Fraccao &a, const Fraccao &b);
ostream & operator<<(ostream & saida, const Fraccao & fra);
而在我的主要:
void func(int n) {
cout << n; //
}
int main(int argc, char** argv) {
//...
d = a + b;
const Fraccao h(7, 3);
func(h);
return 0;
}