可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've written an operator<< for my templated class:
template<class T>
std::ostream& operator<<(std::ostream &strm, const MyClass<T> &obj)
and when I write
cout << myClassInstance << endl;
this compiles and runs, but my Eclipse CDT says:
Invalid overload of 'endl'
Why does it tell me that?
(I use Eclipse CDT Kepler on Win7 64bit with Cygwin gcc)
回答1:
This is indeed a bug with Eclipse CDT (more specifically Eclipse's Code Analysis tool CODAN). There is bug report and it has been fixed and should be available from CDT 8.3.0 which is due February 2014.
回答2:
I was getting this error as well.
//print the value
cout << rt->element << endl;
A simple change to:
//print the value
cout << rt->element;
cout << endl;
removed the error for me. New to C++, but it seems like you also need to overload << for myClassInstance. If you want to use the original method.
回答3:
The problem is (as I understand) with the Code Analysis tool of Eclipse.
If you like, you can avoid this message by completely disabling the check for invalid overloads:
- Open Preferences Window (from main menu Window\Preferences)
- Go to C/C++ -> Code Analysis
- At the right pane see "Syntax and Semantic Errors" | "Invalid Overload"
- Un-check the check-box
- Press OK
Then you will see that the error is disappeared.
However it might skip the real errors and it might be better to let it stay checked but use "Customize Selected" button to change its severity level.
I have changed it to "WARNING" instead of "ERROR".
As @plasmaHH said, I think Eclipse could not parse the C++ correctly in this scenario.
回答4:
I know this is an old question, but I encountered a similar issue with Eclipse Neon ( v4.6.0 ) on Ubuntu 16.04 LTS
My code was:
stringstream l_Buffer;
l_Buffer << "test" << endl;
const char* l_Temp = l_Buffer.str().c_str();
eclipse reported 3 errors:
- Invalid overload of 'endl'
- Method 'c_str' could not be resolved
- Method 'str' could not be resolved
I tried a bunch of stuff, rebuilding the index, messing around with the Code Analysis tool (configuring it the same way as my build), and writing std::endl
... All to no avail.
The thing that fixed all three errors in my case was by replacing
stringstream l_Buffer;
with:
basic_stringstream<char> l_Buffer;
Note: Ctrl + Clicking stringstream
leads you to its typedef definition in iosfwd.h
which is:
/// Class for @c char mixed input and output memory streams.
typedef basic_stringstream<char> stringstream;
回答5:
You can try to add the comment // @suppress("Invalid overload")
as in the following line:
cout << "SOME TEXT" << endl; // @suppress("Invalid overload")
This will suppress that problem, keeping still the capability to detect other possible invalid overloads somewhere in the code.
Btw, if you click on the bug on the left side of the editor, Eclipse will do that for you. (Eclipse: Oxygen).
回答6:
even though it's an old question, for future visitors, the thing made the error go away for me was simply use fully qualified cout and endl:
std::cout << thing << std::endl
Not sure why Eclipse complains there, as it compiles and runs fine without namespaces in that particular place in code.
回答7:
try putting std::endl instead of endl
or overload within your class as a member:
template<class T>
MyClass<T>& operator<<( std::ostream&(*f)(std::ostream&) ) {
std::cout << f;
return *this;
}
回答8:
This keeps happening to me when I upgrade the eclipse. It has all the strange error of Member declaration not found
, invalid overload of endl
, Invalid arguments ...
. Now I figured out that it is the info in the workspace and old projects were not up-to-date for the new CDT codan.
For someone has the same problem after upgrading the eclipse or copying workspaces to new platform. It is actually very easy to solve: Project->C/C++ index->Rebuild
. After it is done, all strange errors will go away.