Reputation: 24121
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.
Why is it not compiling?
Update: I am using gcc
, and running gcc --version
prints out:
gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
Upvotes: 24
Views: 59580
Reputation: 429
If you are using Cmake to compile, add line:
"add_definitions(-std=c++11)"
after find_package command.
Upvotes: 0
Reputation: 4553
In libstdc++, the definitions of stoi
, stol
, etc., as well as the to_string
functions, are guarded by the condition
#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
#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.)
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.)
You should check if _GLIBCXX_USE_C99
is not defined, or if _GLIBCXX_HAVE_BROKEN_VSWPRINTF
is defined (which may be the case on MinGW?)
Upvotes: 19
Reputation: 20901
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
.
@ 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 gcc and using g++
Pay attention 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 flag like set(CMAKE_EXE_LINKER_FLAGS "-libgcc -lstdc++")
(Not tested)
#include <cstdlib>
int myInt = std::atoi(test.c_str());
Upvotes: 3
Reputation: 1335
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.
$ 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.
Upvotes: 9