简体   繁体   English

'运算符<<(std :: ostream&,int&)'不明确

[英]'operator<<(std::ostream&, int&)’ is ambiguous

Isn't this: 这不是吗?

operator<<(std::cout, 0);

The same as this? 一样吗

std::cout<<0;

I tried this piece of code: 我尝试了这段代码:

#include<iostream>
int main()
{
    operator<<(std::cout,0);
    return 0;
}

But I got the following error message: 但是我收到以下错误消息:

a.cpp: In function ‘int main()’:
a.cpp:11:28: error: call of overloaded ‘operator<<(std::ostream&, int)’ is ambiguous
a.cpp:11:28: note: candidates are:
/usr/include/c++/4.6/ostream:528:5: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/4.6/ostream:523:5: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/4.6/ostream:510:5: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/4.6/bits/ostream.tcc:323:5: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/ostream:473:5: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
/usr/include/c++/4.6/ostream:468:5: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]
/usr/include/c++/4.6/ostream:462:5: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/include/c++/4.6/ostream:456:5: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]

Can someone explain it please? 有人可以解释吗?

No, it is the same as: 不,与以下内容相同:

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

Using operator<<(std::cout, 0); 使用operator<<(std::cout, 0); makes Argument-Dependent Lookup (ADL) kick in, which finds multiple candidates that accept a std::basic_ostream<char> and an int (or a type that has a valid implicit conversion from int ) as input. 进行依赖参数的查找 (ADL),该查找找到接受std::basic_ostream<char>和一个int (或具有从int进行有效的隐式转换的类型)作为输入的多个候选。 Once ADL kicks in, all of these various overloads become valid candidates. 一旦启用ADL,所有这些重载都将成为有效的候选者。

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

相关问题 std :: ostream&运算符&lt;&lt;(std :: ostream&sstr,const T&val)的模棱两可的重载 - Ambiguous overload of std::ostream& operator<<(std::ostream& sstr, const T& val) std :: ostream&运算符&lt;&lt;类型推导 - std::ostream& operator<< type deduction “friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, LinkedList&amp; list)”是什么意思? - What does “friend std::ostream& operator<<(std::ostream& out, LinkedList& list)” mean? std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const T&amp;) 未被覆盖 - std::ostream& operator<<(std::ostream&, const T&) not being overridden ostream&运算符&lt;&lt;(ostream&(* pf)(ostream&)); - ostream& operator<< (ostream& (*pf)(ostream&)); 重载运算符std :: ostream&operator &lt;&lt;打印实例内存地址 - Overloaded operator std::ostream& operator<< prints instance memory address “覆盖” ostream&运算符&lt; - “Overriding” ostream& operator << 错误:“对运算符&lt;&lt;(std :: ostream&,Dogs const&)的未定义引用” - Error: “Undefined reference to operator<<(std::ostream&, Dogs const&)” 我们可以重载 operator&lt;&lt;,第一个参数的类型是 std::ostream&amp;&amp; 而不是 std::ostream&amp; - Can we overload operator<< with the first parameter being of the type std::ostream&& instead of std::ostream& 重载运算符&lt;&lt;返回ostream& - Overloaded operator<< returning ostream&
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM