C++ std::stoi 在 MinGW 上的 g++ 4.6.1 中不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8542221/
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
std::stoi doesn't exist in g++ 4.6.1 on MinGW
提问by Seth Carnegie
I tried compiling this simple program on IdeOne(which uses gcc 4.5.1) and on my Linux computer (which uses something like 4.6.4):
我尝试在 IdeOne(使用 gcc 4.5.1)和我的 Linux 计算机(使用类似 4.6.4 的东西)上编译这个简单的程序:
#include <string>
#include <iostream>
int main() {
std::cout << std::stoi("32") << std::endl;
}
And it compiles perfectly and outputs 32
. However, when I try to compile it on my windows computer with MinGW and gcc 4.6.1, I get this error:
它可以完美编译并输出32
. 但是,当我尝试使用 MinGW 和 gcc 4.6.1 在我的 Windows 计算机上编译它时,出现此错误:
test.cpp: In function 'int main()':
test.cpp:5:19: error: 'stoi' is not a member of 'std'
The same happens with std::stoul
, etc. Does std::stoi
and family not exist in MinGW for some reason? I thought gcc on MinGW (sh|w)ould behave the same as on Linux.
std::stoul
等也会发生同样的情况。std::stoi
由于某种原因,MinGW 中不存在和家庭吗?我认为 MinGW (sh|w) 上的 gcc 的行为与 Linux 上的相同。
回答by DRH
This is a result of a non-standard declaration of vswprintf
on Windows. The GNU Standard Library defines _GLIBCXX_HAVE_BROKEN_VSWPRINTF
on this platform, which in turn disables the conversion functions you're attempting to use. You can read more about this issue and macro here: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
这是vswprintf
Windows 上非标准声明的结果。GNU 标准库_GLIBCXX_HAVE_BROKEN_VSWPRINTF
在此平台上进行定义,从而禁用您尝试使用的转换函数。您可以在此处阅读有关此问题和宏的更多信息:http: //gcc.gnu.org/bugzilla/show_bug.cgi?id=37522。
If you're willing to modify the header files distributed with MinGW, you may be able to work around this by removing the !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
macro on line 2754 of .../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h
, and adding it back around lines 2905 to 2965 (the lines that reference std::vswprintf
). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be available.
如果您愿意修改随 MinGW 分发的头文件,您可以通过删除 的!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
第 2754 行上的宏.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h
并将其添加回第 2905 至 2965 行(引用 的行std::vswprintf
)来解决此问题。您将无法使用这些std::to_wstring
函数,但许多其他转换函数应该可用。
回答by M.M
This is fixed in MinGW-w64, a fork of the original MinGW project that actually is interested in fixing bugs like this. It was fixed as of g++ 4.9.2, and maybe earlier.
这在MinGW-w64 中得到了修复,MinGW-w64是原始 MinGW 项目的一个分支,实际上有兴趣修复这样的错误。它从 g++ 4.9.2 开始修复,也许更早。
Note: for people coming here who have done a default install of CodeBlocks (which comes with the old, broken MinGW), and want to upgrade the compiler, see this answer.
注意:对于已经默认安装 CodeBlocks(旧的、损坏的 MinGW 随附)并希望升级编译器的人,请参阅此答案。
You can use any build of MinGW-w64: I use the self-installer from mingw-builds.org, whereas that answer uses TDM-GCC-64. If you want both 64bit and 32bit compilation you need to install and add 2 new compilers: mingw-w64 64-bit, and mingw-w64 32-bit. It does NOT support using one installation of g++ with the -m32
or -m64
switch to toggle.
您可以使用任何版本的 MinGW-w64:我使用来自 mingw-builds.org 的自安装程序,而该答案使用 TDM-GCC-64。如果您需要 64 位和 32 位编译,则需要安装并添加 2 个新编译器:mingw-w64 64 位和 mingw-w64 32 位。它不支持使用一个 g++ 安装与-m32
或-m64
开关切换。
回答by ashish
I am using MinGW 4.9.3-1. This problem seems to be still there. As a workaround, I used the another way of getting integers from strings.
我正在使用 MinGW 4.9.3-1。这个问题似乎仍然存在。作为一种解决方法,我使用了另一种从字符串中获取整数的方法。
int rows, columns;
sscanf(argv[1], "%d", &rows);
sscanf(argv[2], "%d", &columns);
回答by Sean Nolan
Use Mingw-w64. I had this same issue and using Mingw-w64 worked for me.
使用 Mingw-w64。我遇到了同样的问题,使用 Mingw-w64 对我有用。