我在嘗試編譯下面的簡單程序時收到編譯錯誤。在使用ubuntu的C++中std :: stoi存在問題
error: ‘stoi’ was not declared in this scope
我一直在努力,包括#include <string>
和#include <string.h>
,我仍然感到有這些問題。我使用的是Ubuntu,但我不記得我是如何安裝g ++的,但我確定它使用的是apt-get install g ++命令,所以我不知道我使用的是什麼版本的g ++或C++庫。
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct Data
{
string fname;
string lname;
int age;
};
int main()
{
bool toContinue = true;
Data data;
string buffer;
do
{
try
{
getline(cin,data.fname);
getline(cin,data.lname);
getline(cin,buffer);
data.age = stoi(buffer);
cout<<data.fname<<" ";
cout<<data.lname<<" ";
cout<<data.age<<endl;
}
catch(std::invalid_argument)
{
cerr<<"Unable to parse integer";
}
}while(toContinue);
return 0;
}
我的目標是能夠在用戶輸入任何變量的垃圾時使用異常處理。
它是C++ 11-only,所以用'-std = C++ 11'編譯。此外,'string.h'是C字符串頭,與'std :: string'無關。 – chris 2013-03-18 01:00:40