Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: function 'to_string' could not be resolved [duplicate]

Possible Duplicate:
Turn off eclipse errors (that arent really errors)

I'm facing this annoying issue: Eclipse refuses to recognize the std::to_string function, but my program compiles without errors. What am I missing?

According to cppreference the std::to_string function is defined in <string>, so I included it explicitly in the incriminated .cpp file. I also tried this, this and this solutions, with no luck.

Any other suggestions?

EDIT:

I'm using g++ 4.7.2 under Linux.

like image 599
Avio Avatar asked Jan 17 '13 14:01

Avio


2 Answers

UPDATE: It's been a long time since I posted the original answer and it has become outdated. I double-checked today (Mar 15, 2014): in Eclipse Kepler (Build id 20130614-0229) it is sufficient to

  • add under Project > Properties > C/C++ Build > Settings then on the Tool Settings tab GCC C++ Compiler > Miscellaneous the -std=c++11 flag,

  • then under Window > Preferences > C/C++ > Build > Settings on the Discovery tab chose CDT GCC Built-in Compiler Settings and add the -std=c++11 flag to Command to get compiler specs. On my machine it looks like this after the change:

    ${COMMAND} -E -P -v -dD -std=c++11 "${INPUTS}"

  • clean and rebuild both your project and your index (Project > C/C++ Index > Rebuild) as Eclipse tends to cache error messages and show them even though they are gone after changing the settings.

This works on my machine for sure. If it doesn't on yours, then you might want to give a shot to this: C++11 full support on Eclipse although I am neither sure about the correctness of this approach nor was it necessary to do it on my machine. As of March 7, 2014 users claim that it helped them whereas the above approach didn't.


The original post, now outdated:

It seems like you have run into the common problem with Codan, see my answer here.


It isn't 100% clear how the code compiles. Within Eclipse? Or from command line, properly setting the flags? So just in case:

You are using a C++11 function. Do you pass the -std=c++0x or the -std=c++11 flags to the compiler (assuming gcc)?

You might have to also add __GXX_EXPERIMENTAL_CXX0X__ to your defines (again, assuming gcc) and restart Eclipse.

like image 91
Ali Avatar answered Nov 03 '22 06:11

Ali


In my case eclipse believes __cplusplus is defined to 199711L but I'm quite certain that this should be defined to something along the lines of 201103L because the libstdc++ v3 uses

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else

in most of the new C++11 headers such as <future> and basic_string.h (where the definition of std::to_string is) which is included by <string>. Although when compiling with g++ (Built by MinGW-builds project) 4.8.0 20121225 (experimental) I get absolutely no error. This strange behaviour apparently confuses eclipse and makes it fail to properly phrase the included files.

Defining __cplusplus to something over 201103L before including the C++11 files should fix the bogus eclipse syntax errors such as Symbol 'shared_ptr' could not be resolved.

#undef __cplusplus
#define __cplusplus 201900L

After making the redefinition you'll want to right click on the project Index -> Rebuild & Freshen all files or even better restart eclipse all together.

like image 10
Edward A Avatar answered Nov 03 '22 05:11

Edward A