std::stoi not recognized by eclipse

核能气质少年 提交于 2020-01-13 19:50:59

问题


On my system, running Windows 7 x64, Eclipse Luna, and g++ 4.9.2 (installed via cygwin), it seems std::stoi was never declared by g++. According to the documentation, stoi is part of the string library, so obviously I have #include <string>.

In addition, I know that stoi was introduced in C++11, and I have set the appropriate flags for my compiler (g++), even though this seems like an IDE error, rather than a compiler error.

Still, I would get one of the following error messages when building my project:

error: 'stoi' is not a member of 'std'
error: Function 'stoi' could not be resolved

How do I fix this? How do I make Eclipse recognize stoi?


回答1:


This took quite a bit of digging, but apparently, the code declaring stoi is located inside <basic_string.h>, and it kind of looks like this:

#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
#include <ext/string_conversions.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
  // 21.4 Numeric Conversions [string.conversions].
  inline int
  stoi(const string& __str, size_t* __idx = 0, int __base = 10)
  { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
                __idx, __base); }

So, it's quite easy to see that in order to have std::stoi available, one would have to make sure the following:

  • __GXX_EXPERIMENTAL_CXX_0X__ is defined.
  • _GLIBCXX_USE_C99 is defined.
  • _GLIBCXX_HAVE_BROKEN_VSWPRINTF is NOT defined.

Now, in Eclipse, if you have auto-completion turned on, checking for these and setting them up is quite easy!

Checking: Simply start typing them anywhere in the code, on a new line, and see if the auto-completion (usually CTRL+Space) suggests them. If it does - they're defined. If they're not, keep reading.

Defining is easy as well! Navigate to: Project -> Properties -> C/C++ General -> Paths and Symbols -> Symbols tab.

Click on GNU C++ from the options list to the left, and then click on Add, and simply give the name needed.

Click okay and close the properties window, after you've added the required definitions, and rebuild the indexer by navigating to *Project -> C/C++ Index -> Rebuild.

P.S. While you're at it, you may want to also define the following symbol: __cplusplus with value 201103L.

(This will help set various other options, such as std::unordered_set, for instance).




回答2:


It's hidden behind <basic_string.h> - it's just your env is not configured to expose that piece of code.

When you have a Makefile project, the Eclipse indexer sometimes needs help knowing the environment in order to match your Makefile. Because of this, it is possible to successfully build your project using make even though eclipse will show errors.

For this particular issue, you basically want your __cplusplus macro to be set to something >= 201103L. Adding -std=c++11 to your makefile does this for your make environment, but we have to help Eclipse out as well.

For me, Eclipse still had it set to 199711L, even after following hyit's directions.

To fix:

Project -> Properties -> C/C++ General -> Preprocessor Include Paths, Macros etc -> CDT GCC Built-in Compiler Settings [ Shared ] -> Add -std=c++11 after ${COMMAND}

Rebuild your project's index and there you go. It should work.



来源:https://stackoverflow.com/questions/32309095/stdstoi-not-recognized-by-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!