C ++ - stoi()未在此范围内声明

时间:2015-06-05 11:27:04

标签: c++ c++11 gcc compiler-errors codeblocks

我正在通过编写来自用户的简单输入/输出来练习我的C ++。我现在一直在搜索如何将字符串转换为int的一个多小时。每个答案都不同,需要其他东西。

我正在使用Code :: Blocks v.13.12。 MingW32文件夹被添加到环境变量中。我最终使用stoi将我的字符串转换为带有try / catch块的整数。我通过转到Settings -> Compiler并勾选Have g++ follow the C++11 ISO language standard [-std=c++11]在Code :: Blocks中添加了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))

源代码,按要求删除:已删除,因为它与解决方案无关。

1 个答案:

答案 0 :(得分:0)

我设法让它发挥作用!我从http://nuwen.net/mingw.html下载了最新的Mingw32版本(GCC 5.1.0编写)。

我将环境变量更改为<your path>\MinGW\bin并运行g++ -std=c++11 -static main.cpp -o test.exe。工作就像一个魅力。仍然不知道C :: B是如何做到的,虽然它不能通过命令行工作,即使它们指向同一个文件夹。