我已经为我的模板类写了一个运算符<<:
template<class T>
std::ostream& operator<<(std::ostream &strm, const MyClass<T> &obj)
当我写作时
cout << myClassInstance << endl;
这段代码可以编译和运行,但我的Eclipse CDT显示:
'endl'的重载无效
为什么会这样提示?
(我在Win7 64位上使用Eclipse CDT Kepler和Cygwin gcc)
我已经为我的模板类写了一个运算符<<:
template<class T>
std::ostream& operator<<(std::ostream &strm, const MyClass<T> &obj)
当我写作时
cout << myClassInstance << endl;
这段代码可以编译和运行,但我的Eclipse CDT显示:
'endl'的重载无效
为什么会这样提示?
(我在Win7 64位上使用Eclipse CDT Kepler和Cygwin gcc)
我也遇到了这个错误。
//print the value
cout << rt->element << endl;
简单更改为:
//print the value
cout << rt->element;
cout << endl;
帮我解决了错误。虽然我是C++的新手,但似乎你还需要为myClassInstance重载<<运算符,如果你想使用原始方法。
operator<<()
。 - Brandlingo// @suppress("Invalid overload")
:cout << "SOME TEXT" << endl; // @suppress("Invalid overload")
stringstream l_Buffer;
l_Buffer << "test" << endl;
const char* l_Temp = l_Buffer.str().c_str();
eclipse报告了3个错误:
我尝试了很多东西,重建索引,玩弄代码分析工具(将其配置成与我的构建方式相同),并编写std::endl
...但都没有成功。
在我这种情况下解决所有三个错误的方法是替换:
stringstream l_Buffer;
使用:
basic_stringstream<char> l_Buffer;
stringstream
会导航到其在 iosfwd.h
中的 typedef 定义,如下所示:/// Class for @c char mixed input and output memory streams.
typedef basic_stringstream<char> stringstream;
template<class T>
MyClass<T>& operator<<( std::ostream&(*f)(std::ostream&) ) {
std::cout << f;
return *this;
}
项目->C/C++索引->重建
。完成后,所有奇怪的错误都会消失。尽管这是一个老问题,但对于未来的访问者,让错误消失的方法就是简单地使用完全限定的cout和endl:
std::cout << thing << std::endl
不确定为什么Eclipse在那里抱怨,因为在代码的那个特定位置没有命名空间时,它可以编译和运行得很好。
Window
→Preferences
→C/C++
→Indexer
下的索引设置。 - Jesse Good