【问题标题】:C++ - stoi() was not declared in this scopeC++ - stoi() 未在此范围内声明
【发布时间】: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 &lt;string&gt;了吗?您是否使用正确的范围 std::stoi 为其添加前缀?
  • @CoryKramer 我已经包含了一堆库,包括 。我也在使用using namespace std;。它在 Code::Blocks 中运行良好,并且一切正常。但不是通过命令行。
  • 请不要在没有SSCCE 和重现问题所需的所有信息的情况下发布“为什么我的代码不起作用” 问题。
  • @BaummitAugen 对不起,不习惯 stackoverflow。编辑了我的帖子。
  • @theusual,如果代码不是很长(我认为不是),你能粘贴 all 源代码吗?

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2021-07-17
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多