stoi出错,用gdb调试
Error with stoi and debugged with gdb
我的 OS 是 ubuntu 14.04,笔记本电脑,i7。
g++ 版本为 g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
。
我试过运行一个简单的代码来测试stoi
:
#include <string>
int main()
{
std::string s = "123";
int i = std::stoi(s);
}
当我编译它时:g++ -g prueba2.cpp
,我得到:
prueba2.cpp: In function ‘int main()’:
prueba2.cpp:6:12: error: ‘stoi’ is not a member of ‘std’
int i = std::stoi(s);
^
当我先使用 g++ -std=c++0x -g prueba2.cpp
(我也尝试使用 -std=c++11)然后使用 dbg
调试它两次时,我得到:
然后,我也进行了简单的搜索,并按照here1, here2 and here3中的建议进行了操作,none成功了。
我是不是在做傻事?
std::stoi
是 C++11 的特性。因此,如果您使用 -std=c++11
标志(或您提到的等效 -std=c++0x
标志,它 nothing 与调试无关,那么您的代码只能编译。
您提供的终端会话还显示编译可以使用这些标志,并且您的程序运行良好,没有任何问题。如果你想打印解析结果,你可以这样做:std::cout << i << std::endl
如果您不想使用 C++11 功能,可以使用 >>
流运算符将您的字符串解析为 int
:
stringstream ss(s);
int i;
ss >> n;
但请注意:除了 stoi
,如果您的输入不包含有效数字,您不会得到异常。您必须自己检查流的状态。
是的,我认为你在做一些很愚蠢的事情。您可能编译了第一个代码,它没有 std::cout
语句,并且您可能在没有 -std=c++11
的情况下执行了编译步骤,这将导致 std::stoi
不被包含,因为 std::stoi
来自 C++11 及以后。结果仍然是旧的可执行文件,什么也不打印。
使用 -std=c++11
重新编译并确保您正确保存了文件。 Your code clearly works.
注意:Windows 上 MinGW 的 GCC vanilla 端口存在缺陷,并且有一些与 C++11 及更高版本相关的错误;使用 MinGW-w64,如果你决定在 Windows 上编译,可以帮助解决这个问题。
我的 OS 是 ubuntu 14.04,笔记本电脑,i7。
g++ 版本为 g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
。
我试过运行一个简单的代码来测试stoi
:
#include <string>
int main()
{
std::string s = "123";
int i = std::stoi(s);
}
当我编译它时:g++ -g prueba2.cpp
,我得到:
prueba2.cpp: In function ‘int main()’:
prueba2.cpp:6:12: error: ‘stoi’ is not a member of ‘std’
int i = std::stoi(s);
^
当我先使用 g++ -std=c++0x -g prueba2.cpp
(我也尝试使用 -std=c++11)然后使用 dbg
调试它两次时,我得到:
然后,我也进行了简单的搜索,并按照here1, here2 and here3中的建议进行了操作,none成功了。
我是不是在做傻事?
std::stoi
是 C++11 的特性。因此,如果您使用 -std=c++11
标志(或您提到的等效 -std=c++0x
标志,它 nothing 与调试无关,那么您的代码只能编译。
您提供的终端会话还显示编译可以使用这些标志,并且您的程序运行良好,没有任何问题。如果你想打印解析结果,你可以这样做:std::cout << i << std::endl
如果您不想使用 C++11 功能,可以使用 >>
流运算符将您的字符串解析为 int
:
stringstream ss(s);
int i;
ss >> n;
但请注意:除了 stoi
,如果您的输入不包含有效数字,您不会得到异常。您必须自己检查流的状态。
是的,我认为你在做一些很愚蠢的事情。您可能编译了第一个代码,它没有 std::cout
语句,并且您可能在没有 -std=c++11
的情况下执行了编译步骤,这将导致 std::stoi
不被包含,因为 std::stoi
来自 C++11 及以后。结果仍然是旧的可执行文件,什么也不打印。
使用 -std=c++11
重新编译并确保您正确保存了文件。 Your code clearly works.
注意:Windows 上 MinGW 的 GCC vanilla 端口存在缺陷,并且有一些与 C++11 及更高版本相关的错误;使用 MinGW-w64,如果你决定在 Windows 上编译,可以帮助解决这个问题。