为什么对 operator<< 的显式调用不明确?
Why is explicit call to operator<< ambiguous?
这是简单的代码:
int main()
{
int x=0;
std::cout<<x;
operator<<(std::cout,x); //ambiguous
return 0;
}
为什么 operator<<(std::cout,x)
调用不明确但 std::cout<<x;
不明确?谢谢
这里的问题是,对于输出整数,operator<<
is an std::ostream
member function。
因此,要显式调用运算符函数,您应该这样做,例如
std::cout.operator<<(x);
stand-alone operator<<
function 用于字符和字符串。
这是简单的代码:
int main()
{
int x=0;
std::cout<<x;
operator<<(std::cout,x); //ambiguous
return 0;
}
为什么 operator<<(std::cout,x)
调用不明确但 std::cout<<x;
不明确?谢谢
这里的问题是,对于输出整数,operator<<
is an std::ostream
member function。
因此,要显式调用运算符函数,您应该这样做,例如
std::cout.operator<<(x);
stand-alone operator<<
function 用于字符和字符串。