【发布时间】:2013-08-17 05:49:09
【问题描述】:
我正在处理的任务有问题。我们正在编写一个在它自己的命名空间中定义的复数类。除了 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&)
我在这里花了一些时间阅读论坛,偶然发现了一个关于名称隐藏的答案,但我似乎无法让它与我的代码一起使用。非常感谢任何帮助!
【问题讨论】:
-
定义应该真正进入命名空间imo。那么这两个签名都不需要
maths::部分,它应该仍然可以工作。 -
您能详细说明一下吗?您的意思是不要将它们声明为友元函数(因为我试图访问的值是公开的,所以我可以这样做)?还是以不同的方式进行?
-
如果它不需要访问私人部分,那么是的,它绝对不应该是一个朋友功能。我的意思是将实际重载放入您的
maths命名空间,因为它与类一起使用。 ADL 会找到它。但是,您的观点意味着类内不需要声明。
标签: c++ class namespaces overloading istream