C++ Eclipse:无法解析函数“to_string”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14381137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:18:12  来源:igfitidea点击:

Eclipse: function 'to_string' could not be resolved

c++eclipsec++11eclipse-cdt

提问by Avio

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

可能的重复:
关闭 eclipse 错误(这不是真正的错误)

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

我正面临这个烦人的问题:Eclipse 拒绝识别该std::to_string函数,但我的程序编译时没有错误。我错过了什么?

According to cppreferencethe std::to_stringfunction is defined in <string>, so I included it explicitly in the incriminated .cppfile. I also tried this, thisand thissolutions, with no luck.

根据cppreference,std::to_string函数在 中定义<string>,因此我将其明确包含在被指控的.cpp文件中。我也试过这个这个这个解决方案,但没有运气。

Any other suggestions?

还有其他建议吗?

EDIT:

编辑:

I'm using g++ 4.7.2 under Linux.

我在 Linux 下使用 g++ 4.7.2。

回答by Ali

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

更新:自从我发布原始答案以来已经很长时间了,它已经过时了。我今天(2014 年 3 月 15 日)再次检查:在 Eclipse Kepler(构建 ID 20130614-0229)中,足以

  • add under Project > Properties > C/C++ Build > Settingsthen on the Tool Settingstab GCC C++ Compiler > Miscellaneousthe -std=c++11flag,

  • then under Window > Preferences > C/C++ > Build > Settingson the Discoverytab chose CDT GCC Built-in Compiler Settingsand add the -std=c++11flag 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 andyour 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.

  • 下添加项目>属性> C / C ++编译>设置然后在工具设置选项卡 GCC C ++编译器>其它-std=c++11标志,

  • 然后在发现选项卡上的Window > Preferences > C/C++ > Build > Settings 下选择CDT GCC Built-in Compiler Settings并将标志添加到Command 以获取编译器规范。在我的机器上,更改后看起来像这样: -std=c++11

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

  • 清理并重建您的项目索引(Project > C/C++ Index > Rebuild),因为 Eclipse 倾向于缓存错误消息并显示它们,即使它们在更改设置后消失了。

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 Eclipsealthough 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 claimthat it helped them whereas the above approach didn't.

这肯定适用于我的机器。如果它不在你的身上,那么你可能想试一试:C++11 对 Eclipse 的完全支持,尽管我不确定这种方法的正确性,也没有必要在我的机器上这样做。截至 2014 年 3 月 7 日,用户声称它帮助了他们,而上述方法没有。



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 howthe 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++0xor the -std=c++11flags to the compiler (assuming gcc)?

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

您似乎遇到了 Codan 的常见问题,请在此处查看我的回答


代码如何编译并不是 100% 清楚。在 Eclipse 中?或者从命令行正确设置标志?所以以防万一:

您正在使用 C++11 函数。您是否将标志-std=c++0x-std=c++11标志传递给编译器(假设为 gcc)?

您可能还必须添加__GXX_EXPERIMENTAL_CXX0X__到您的定义中(再次假设是 gcc)并重新启动 Eclipse。

回答by Edward A

In my case eclipse believes __cplusplusis defined to 199711Lbut I'm quite certain that this should be defined to something along the lines of 201103Lbecause the libstdc++ v3uses

在我的情况下,eclipse相信__cplusplus被定义为,199711L但我很确定这应该被定义为类似的东西,201103L因为libstdc++ v3使用

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

在大多数新的 C++11 头文件中,例如<future>basic_string.h(std::to_string 的定义所在),它包含在<string>. 虽然在编译时g++ (Built by MinGW-builds project) 4.8.0 20121225 (experimental)我绝对没有错误。这种奇怪的行为显然混淆了 eclipse 并使其无法正确地表达所包含的文件。

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

在包含 C++11 文件之前定义__cplusplus一些东西201103L应该可以修复虚假的 Eclipse 语法错误,例如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 filesor even better restart eclipse all together.

重新定义后,您需要右键单击该项目Index -> Rebuild & Freshen all files,甚至更好地一起重新启动 eclipse。