Stoi未在作用域中声明-代码::块



编辑:我试图通过单击编译器标志中的"让g++遵循C++11 ISO C++语言标准"来告诉它使用C++11。

我发现stoi没有在scope中声明,并且我已经将c++11添加到Code::Blocks;中。我已经在设置->编译器->编译器标志中添加了兼容性,但它仍然会给我带来错误。

当我尝试做atoi或strtol时,我会得到以下错误:

C: \Users\user\Desktop\Programing\NewProject\main.cpp|19|错误:无法将"std::string{aka std::basic_string}"转换为"const char*"对于参数"1"到"long int strtol(const char*,char**,int)"|

我的代码:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
    string numberGuessed;
    int numberGuessedint = 0;
    do {
        cout << "Guess a number between 1 and 10: ";
        getline(cin, numberGuessed);
        numberGuessedint = stoi(numberGuessed);
        cout << numberGuessedint << endl;

    } while(numberGuessedint != 4);
    cout << "You win!" << endl;
    return 0;
}

这是与Code::Blocks捆绑在一起的MinGW中的一个已知错误。

您可以应用修补程序:http://tehsausage.com/mingw-to-string

或者下载新版本的MinGW(最好支持线程,因为你也缺乏它),并替换你现在拥有的一个。

要使用atoi,您需要:

        numberGuessedint = atoi(numberGuessed.c_str());

我正在编写一个对我有用的解决方案。正如我在大多数发布在堆栈溢出上的解决方案中发现的那样,早期版本的代码块包含一个错误。所以我删除了旧的代码块版本,并从代码块网站安装了新的17.12版本。

然后我只点击编译器标志中的"让g++遵循C++11 ISO C++语言标准"。

设置->编译器->编译器标志。

它对我有效(我使用的是windows7)。

最新更新