Compilation error: 'stoi' is not a member of 'std'

My code is:

#include <iostream> #include <string> int main() { std::string test = "45"; int myint = std::stoi(test); std::cout << myint << '\n'; } 

Gives me a compilation error:

 error: 'stoi' is not a member of 'std' int myint = std::stoi(test); ^ 

However, according to here , this code should compile in order. I use the set(CMAKE_CXX_FLAGS "-std=c++11 -O3") line set(CMAKE_CXX_FLAGS "-std=c++11 -O3") in my CMakeLists.txt file.

Why doesn't it compile?


Update: I am using gcc , and running gcc --version outputs:

 gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010 
+6
source share
3 answers

In libstdC ++, the definitions of stoi , stol , etc., as well as the functions to_string are protected by the condition

 #if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \ && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)) 

I had a failure on one platform before (namely, Termux on Android), as a result, to_string not available even with g ++ 6.1 and the C ++ 14 standard. In this case, I just did

 #define _GLIBCXX_USE_C99 1 

before incorporating anything, and voilà, suddenly functions existed. (You should put this first or even on the command line, and not just before turning on <string> , because another header may include <string> first, and then its included guards will keep it from viewing your macro. )

I have not investigated why this macro was not installed in the first place. Obviously, this is troubling if you want your code to really work (in my case, I'm not really, but FWIW had no problems.)

You should check if _GLIBCXX_USE_C99 defined or _GLIBCXX_USE_C99 defined (what could be the case with MinGW?)

+8
source

std :: stoi is a C ++ 11 function . You must use -std=c++11 to enable it in both g ++ and clang ++. This is the actual problem, not a binding error or a specific preprocessor.

  $ 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 you bring this into your compilation options? Check the generated makefile and make sure that the commands that are executed are defined.

+5
source

Your version seems relevant, so there should be no problem. I think this may be due to gcc . Instead, try g++ (most likely automatically linking the problem. If you just run gcc in a C ++ file, it won’t “just work” like g ++. This is because it will not automatically link to the std C ++ library and etc.). My second tip: try std::atoi .

@ I fixed the problem. std::stoi uses libstdc ++ . This is a standard C ++ library for GNU . In gcc you need to link the addition of -lstdc++ . However, in g ++, libstdc ++ is bound automatically. using gcc and using g ++

Pay attention to how it is compiled.

using 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

I think you should set the flag as set(CMAKE_EXE_LINKER_FLAGS "-libgcc -lstdc++") (not verified)

 #include <cstdlib> int myInt = std::atoi(test.c_str()); 
+2
source

All Articles