C ++ Stoi无法使用Eclipse进行编译

时间:2018-09-02 14:42:45

标签: c++ eclipse

此代码无法编译,并显示无法解决stio的错误。我在这里犯了一些新手错误吗?

Eclipse版本:3.8.1 Mint KDE应该都是最新的。

GCC版本:gcc(Ubuntu 5.4.0-6ubuntu1〜16.04.10)5.4.0 20160609

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

int main() {
    string numberGuessed;
    int intNumberGuessed = 0;
    int answer = 0;

    answer = (rand() % 100) + 1;

    do {
        cout << "Guess a number "; // prints !!!Hello World!!!
        getline(cin, numberGuessed);
        intNumberGuessed = stoi(numberGuessed);
        cout << "You guessed "<< numberGuessed << endl;
        cout << "You are not correct. Try again" << endl;
    } while (answer != intNumberGuessed);

cout << "you got it";
return 0;
}

错误消息。

16:39:14 **** Incremental Build of configuration Debug for project Hello2 ****
make all 
Building file: ../src/Hello2.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Hello2.d" - 
MT"src/Hello2.d" -o "src/Hello2.o" "../src/Hello2.cpp"
../src/Hello2.cpp: In function ‘int main()’:
 ../src/Hello2.cpp:27:40: error: ‘stoi’ was not declared in this scope
  intNumberGuessed = stoi(numberGuessed);
                                    ^
make: *** [src/Hello2.o] Error 1
src/subdir.mk:18: recipe for target 'src/Hello2.o' failed

16:39:14 Build Finished (took 613ms)

1 个答案:

答案 0 :(得分:2)

std::stoi函数自c ++ 11标准以来可用。

显然,您的GCC编译器版本太旧,无法将c ++ 11作为当前的默认标准。

您可以尝试指定-std=c++11-std=c++0x编译器标志,或者将您的gcc编译器更新为最新版本之一。

这里有link,详细说明了如何设置编译器标志。

This可能会帮助您将编译器版本更新为最新版本。

相关问题