【发布时间】:2015-08-20 08:02:54
【问题描述】:
我正在通过编写来自用户的简单输入/输出来练习我的 C++。我现在已经搜索了一个多小时关于如何将字符串转换为 int 的问题。每个答案都是不同的,并且需要其他答案。
我正在使用 Code::Blocks v.13.12。 MingW32 文件夹被添加到环境变量中。我最终使用 stoi 将我的字符串转换为带有 try/catch 块的整数。我在 Code::Blocks 中添加了 C++11 支持,方法是转到 Settings -> Compiler 并勾选 Have g++ follow the C++11 ISO language standard [-std=c++11]。 它在 CB 中编译并运行良好,但是当我尝试在命令行中手动编译时出现此错误:
error: 'stoi' was not declared in this scope。它所指的行:characters = stoi(input);
这是我迄今为止尝试过的命令:
g++ -std=c++11 main.cpp & g++ -std=c++11 -c main.cpp -o main.o & mingw32-g++ -std=c+11 -c main.cpp -o main.o(根据输出日志,这是CB使用的)
如果你有比stoi()更好的例子,请尽量简单,加上try/catch的全文。
编辑:这写在我的文件顶部:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <algorithm>
#include <stdexcept>
using namespace std;
错误信息指的是:
int player;
string input;
cin >> input;
player = stoi(input)
My Code::Blocks 编译器输出此日志:
mingw32-g++.exe -std=c++11 -c "C:\Users\<name>\#C++\main.cpp" -o "C:\Users
<name>\#C++\main.o"
mingw32-g++.exe -o "C:\Users\<name>\#C++\main.exe" "C:\Users
<name>\#C++\main.o"
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
源代码,按要求:删除,因为它与解决方案无关。
【问题讨论】:
-
你
#include <string>了吗?您是否使用正确的范围std::stoi为其添加前缀? -
@CoryKramer 我已经包含了一堆库,包括
。我也在使用 using namespace std;。它在 Code::Blocks 中运行良好,并且一切正常。但不是通过命令行。 -
请不要在没有SSCCE 和重现问题所需的所有信息的情况下发布“为什么我的代码不起作用” 问题。
-
@BaummitAugen 对不起,不习惯 stackoverflow。编辑了我的帖子。
-
@theusual,如果代码不是很长(我认为不是),你能粘贴 all 源代码吗?
标签: c++ c++11 gcc compiler-errors codeblocks