使用 C++ 中的此模板重载 operator<<
template<typename T> cEnvir& operator<<(const T& t) {out << t; return *this;}
我有这个模棱两可的错误。
error: ambiguous overload for ‘operator<<’ in ‘((cEnvir*)this)->cEnvir::out << t’
/usr/include/c++/4.2.1/ostream:169: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>] <near match>
/usr/include/c++/4.2.1/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
...
对于类型名 T,问题似乎是编译器无法决定匹配。当我注释掉时/* out << t; */
,编译工作正常。
我该如何解决这个问题?我用gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
.