C++ 编译错误:“stoi”不是“std”的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38034197/
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
Compile error: 'stoi' is not a member of 'std'
提问by Karnivaurus
My code:
我的代码:
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = std::stoi(test);
std::cout << myint << '\n';
}
Gives me the compile error:
给我编译错误:
error: 'stoi' is not a member of 'std'
int myint = std::stoi(test);
^
However, according to here, this code should compile fine. I am using the line set(CMAKE_CXX_FLAGS "-std=c++11 -O3")
in my CMakeLists.txt
file.
但是,根据here,此代码应该可以正常编译。我set(CMAKE_CXX_FLAGS "-std=c++11 -O3")
在我的CMakeLists.txt
文件中使用这一行。
Why is it not compiling?
为什么不编译?
Update: I am using gcc
, and running gcc --version
prints out:
更新:我正在使用gcc
,并运行gcc --version
打印出来:
gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
回答by Nick Matteo
In libstdc++, the definitions of stoi
, stol
, etc., as well as the to_string
functions, are guarded by the condition
在 libstdc++ 中stoi
,stol
、 等的定义以及to_string
函数都受条件保护
#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
&& !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
I have had this fail on one platform before (namely Termux on Android), resulting in to_string
not being available even with g++ 6.1 and the C++14 standard. In that case, I just did
我之前在一个平台(即 Android 上的 Termux)上遇到过这种失败,导致to_string
即使使用 g++ 6.1 和 C++14 标准也无法使用。在那种情况下,我只是做了
#define _GLIBCXX_USE_C99 1
before including anything, and voilà, suddenly the functions existed. (You should put this first, or even on the command line, rather than just before including <string>
, because another header may include <string>
first, and then its include guards will keep it from ever seeing your macro.)
在包括任何东西之前,瞧,功能突然就存在了。(你应该把它放在最前面,甚至放在命令行上,而不是仅仅放在<string>
include之前,因为另一个标题可能<string>
首先包含,然后它的包含守卫会阻止它看到你的宏。)
I did not investigate why this macro wasn't set in the first place. Obviously this is a cause for concern if you want your code to actually work (in my case I didn't particularly, but FWIW there were no problems.)
我没有调查为什么没有首先设置这个宏。显然,如果您希望您的代码实际工作,这是一个令人担忧的问题(在我的情况下我没有特别关注,但 FWIW 没有问题。)
You should check if _GLIBCXX_USE_C99
is not defined, or if _GLIBCXX_HAVE_BROKEN_VSWPRINTF
isdefined (which may be the case on MinGW?)
您应该检查是否_GLIBCXX_USE_C99
未定义,或者是否_GLIBCXX_HAVE_BROKEN_VSWPRINTF
已定义(MinGW 可能是这种情况?)
回答by Taywee
std::stoi is a C++11 function. You have to use the -std=c++11
to enable it in both g++ and clang++. This is the actual issue, not a linking error or a specific preprocessor define.
std::stoi 是一个C++11 函数。您必须使用-std=c++11
来在 g++ 和 clang++ 中启用它。这是实际问题,而不是链接错误或特定的预处理器定义。
$ cat test.cxx
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = std::stoi(test);
std::cout << myint << '\n';
}
$ g++ -otest test.cxx
test.cxx: In Funktion ?int main()?:
test.cxx:7:17: Fehler: ?stoi? ist kein Element von ?std?
int myint = std::stoi(test);
^
$ g++ -otest test.cxx -std=c++11
$ ./test
45
$
edit: I just saw that you used c++11. Are you sure that's making it into your compile options? Check the generated makefile and watch the executed commands to be certain.
编辑:我刚刚看到您使用了 c++11。您确定这将其纳入您的编译选项吗?检查生成的 makefile 并观察执行的命令是否确定。
回答by snr
Your version seems up to date, so there shouldn't be an issue. I think it may be related to gcc
. Try g++
instead.(Most likely automatically linking issue. If you just run gcc on a C++ file, it will not 'just work' like g++ does. That's because it won't automatically link to the C++ std library, etc.). My second advise is try std::atoi
.
您的版本似乎是最新的,所以应该没有问题。我认为可能与gcc
. 试试吧g++
。(很可能是自动链接问题。如果你只是在 C++ 文件上运行 gcc,它不会像 g++ 那样“正常工作”。那是因为它不会自动链接到 C++ std 库等)。我的第二个建议是 try std::atoi
。
@ I have fixed the issue. std::stoi
uses libstdc++. It is about The GNU Standard C++ Library. In gcc
you have to link adding -lstdc++
. However, in g++, libstdc++is linked automatically.
using gccand using g++
@我已经解决了这个问题。std::stoi
使用libstdc++。它是关于GNU 标准 C++ 库的。在gcc
你必须链接添加-lstdc++
. 但是,在 g++ 中,libstdc++是自动链接的。
使用 gcc和使用 g++
Pay attention how it is compiled
注意它是如何编译的
using g++: g++ -std=c++11 -O3 -Wall -pedantic main.cpp && ./a.out
使用 g++: g++ -std=c++11 -O3 -Wall -pedantic main.cpp && ./a.out
using gcc: gcc -std=c++11 -O3 -Wall -pedantic -lstdc++ main.cpp && ./a.out
使用 gcc: gcc -std=c++11 -O3 -Wall -pedantic -lstdc++ main.cpp && ./a.out
I think you should set flag like set(CMAKE_EXE_LINKER_FLAGS "-libgcc -lstdc++")
(Not tested)
我认为你应该设置标志set(CMAKE_EXE_LINKER_FLAGS "-libgcc -lstdc++")
(未测试)
#include <cstdlib>
int myInt = std::atoi(test.c_str());
回答by Zhongyi
If you are using Cmaketo compile, add line:
如果您使用Cmake编译,请添加以下行:
"add_definitions(-std=c++11)"
“添加定义(-std=c++11)”
after find_package command.
在 find_package 命令之后。