23

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)

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • This probably would be an indexing problem. Try rebuilding the index and check your index settings under `Window` → `Preferences` → `C/C++` → `Indexer` – Jesse Good Jul 16 '13 at 10:58
  • 2
    @JesseGood: Settings seem fine, and re-indexing doesn't help. – einpoklum Jul 16 '13 at 11:00
  • There is not enough information to give any more assistance. – Jesse Good Jul 16 '13 at 11:04
  • remember that eclipse can not fully parse c++ – PlasmaHH Jul 16 '13 at 11:14
  • @JesseGood: Did I leave something out? – einpoklum Jul 16 '13 at 13:06
  • @PlasmaHH: But that doesn't 'entitle' it to mark an error; at most a warning... – einpoklum Jul 16 '13 at 13:07
  • @einpoklum: You can always ignore it. If your argument was valid, then it should never mention anything as an error since it could be wrong... – PlasmaHH Jul 16 '13 at 13:23
  • I get the same error when compiling C++ code in Eclipse using MinGW's compiler. However when I compile it at console with MinGW's C++ compiler (4.8.1) I get no error. So it should be about indexing as Jesse Good said. – phoad Oct 20 '13 at 03:36
  • I can only reproduce this in CDT 9 by having an unrelated syntax error higher up in the code. In my case, I had tried to call an auto variable I intended to be an integer as if it were a method. – hoodaticus Jul 26 '17 at 15:06

8 Answers8

14

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:

  1. Open Preferences Window (from main menu Window\Preferences)
  2. Go to C/C++ -> Code Analysis
  3. At the right pane see "Syntax and Semantic Errors" | "Invalid Overload"
  4. Un-check the check-box
  5. 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.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
phoad
  • 1,801
  • 2
  • 20
  • 31
13

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.

frmdstryr
  • 20,142
  • 3
  • 38
  • 32
12

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.

iNFINITEi
  • 1,504
  • 16
  • 23
2

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).

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
dsenic2000
  • 21
  • 2
1

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;
Dargazo
  • 21
  • 2
0

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.

uiron
  • 890
  • 11
  • 14
0

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;
}
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

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.

Wang
  • 7,250
  • 4
  • 35
  • 66