简体   繁体   English

为什么显式调用operator <<不明确?

[英]Why is explicit call to operator<< ambiguous?

Here is the simple code : 这是简单的代码:

int main()
{
    int x=0;
    std::cout<<x; 
    operator<<(std::cout,x); //ambiguous

    return 0;
}

Why is the operator<<(std::cout,x) call ambiguous but not std::cout<<x; 为什么operator<<(std::cout,x)调用不明确,而不是std::cout<<x; ? Thanks 谢谢

The problem here is that for outputting integers, operator<< is an std::ostream member function . 这里的问题是,对于输出整数, operator<<std::ostream 成员函数

So to explicitly call the operator function you should do eg 因此,要显式调用operator函数,您应该执行例如

std::cout.operator<<(x);

The stand-alone operator<< function is for characters and strings. 独立operator<<函数用于字符和字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM