编译错误:“stoi”不是“std”的成员

时间:2022-06-26 16:42:50

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.

然而,根据这里,这段代码应该可以很好地编译。我在CMakeLists中使用了行集(CMAKE_CXX_FLAGS”-std=c+ 11 -O3”)。txt文件。

Why is it not compiling?

为什么不编译?


Update: I am using gcc, and running gcc --version prints out:

更新:我正在使用gcc,并且运行gcc——版本输出:

gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010

3 个解决方案

#1


11  

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

在包括任何东西之前,突然之间,函数就存在了。(您应该把这个放在前面,甚至是在命令行上,而不是在包含 之前,因为另一个header可能首先包含 ,然后它的include警卫将使它永远看不到您的宏)。

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 is defined (which may be the case on MinGW?)

您应该检查是否没有定义_GLIBCXX_USE_C99,或者是否定义了_GLIBCXX_HAVE_BROKEN_VSWPRINTF(这可能是MinGW中的情况?)

#2


5  

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.

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并观察执行的命令是否确定。

#3


2  

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库等)。我的第二个建议是尝试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++

@我已经解决了这个问题。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.o ut

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.o ut

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

#1


11  

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

在包括任何东西之前,突然之间,函数就存在了。(您应该把这个放在前面,甚至是在命令行上,而不是在包含 之前,因为另一个header可能首先包含 ,然后它的include警卫将使它永远看不到您的宏)。

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 is defined (which may be the case on MinGW?)

您应该检查是否没有定义_GLIBCXX_USE_C99,或者是否定义了_GLIBCXX_HAVE_BROKEN_VSWPRINTF(这可能是MinGW中的情况?)

#2


5  

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.

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并观察执行的命令是否确定。

#3


2  

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库等)。我的第二个建议是尝试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++

@我已经解决了这个问题。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.o ut

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.o ut

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