简体   繁体   中英

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

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

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?)

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.

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());

If you are using Cmake to compile, add line:

"add_definitions(-std=c++11)"

after find_package command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM