[英]Problems with std::stoi in C++ using ubuntu
我嘗試編譯下面的簡單程序時收到編譯錯誤。
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中引入的。 您必須使用-std=c++11
選項編譯代碼才能啟用這些功能,因為默認情況下代碼不會編譯為C ++ 11。
Drew評論說如果你使用C ++ 03,你可以使用
事實證明我需要它才能使它工作......
g++ -std=c++0x ./main.cpp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.