我正在处理的任务有问题。我们正在编写一个在它自己的命名空间中定义的复数类。除了 istream 和 ostream 上的重载之外,我一切正常。让我发布一些我的代码:
namespace maths {
class complex_number{
public:
// lots of functions and two variables
friend std::istream& operator >>(std::istream &in, maths::complex_number &input);
}
}
std::istream& operator >>(std::istream &in, maths::complex_number &input)
{
std::cout << "Please enter the real part of the number > ";
in >> input.a;
std::cout << "Please enter the imaginary part of the number > ";
in >> input.b;
return in;
}
int main(int argc, char **argv)
{
maths::complex_number b;
std::cin >> b;
return 0;
}
我得到的错误如下:
com.cpp: In function ‘int main(int, char**)’:
com.cpp:159:16: error: ambiguous overload for ‘operator>>’ in ‘std::cin >> b’
com.cpp:159:16: note: candidates are:
com.cpp:131:15: note: std::istream& operator>>(std::istream&, maths::complex_number&)
com.cpp:37:26: note: std::istream& maths::operator>>(std::istream&, maths::complex_number&)
我花了一些时间在这里阅读论坛并偶然发现了一个关于名称隐藏的答案,但我似乎无法让它与我的代码一起使用。任何帮助是极大的赞赏!