2017-10-03 10 views
1

このコード:この入力で次のエラーの原因は何ですか?

#include <algorithm> 
#include <fstream> 
#include <iostream> 
#include <vector> 
#include <cassert> 
using namespace std; 

const int max_applications_num = 1000; 

enum { AUTHOR, TITLE, VENUE, YEAR, PRESENTATION }; 

vector<vector<string> > database; 

void Tokenize(string line, vector<string> &tokens, string delimiters = "\t ") { 
string token = ""; 
string OneCharString = " "; 
for (size_t i = 0; i < line.size(); i++) 
    if (find(delimiters.begin(), delimiters.end(), line[i]) != 
     delimiters.end()) // line[i] is one of the delimiter characters 
    { 
     if (token != "") 
      tokens.push_back(token); 
     token = ""; 
    } else { 
     OneCharString[0] = line[i]; 
     token += OneCharString; 
    } 

if (token != "") 
    tokens.push_back(token); 
} 

void SaveApplication(const vector<string> &tokens) { 
    database.emplace_back(tokens.begin()+1, tokens.end()); 
} 

void remove_application(size_t pos) { 
    assert(pos < database.size()); 
    database.erase(database.begin()+pos); 
} 

int year_of(vector<string> const &record) { return stoi(record[YEAR]); } 
int year_of(int i) { return year_of(database.at(i)); } 

void sort() { 
    for (size_t j = 0; j <= database.size() - 1; j++) { 

    vector<string> tmp = database.at(j); 

    int tmp_year = year_of(tmp); 

    int i = j - 1; 
    while (i > -1 and year_of(i) > tmp_year) { 
     database.at(i + 1) = database.at(i); 
     i = i - 1; 
    } 

    database.at(i + 1) = tmp; 
    } 
} 

void print() { 
    for (size_t i = 0; i < database.size(); i++) { 
    cout 
     << database.at(i)[AUTHOR] << "\t" 
     << database.at(i)[TITLE] << "\t" 
     << database.at(i)[VENUE] << "\t" 
     << database.at(i)[YEAR] << "\t" 
     << database.at(i)[PRESENTATION] 
     << endl; 
} 
cout << "\n" << endl; 
} 

void ExecuteCommands(const char *fname) { 
ifstream inf; 
inf.open(fname); 

string line; 
while (getline(inf, line).good()) { 
    vector<string> tokens; 
    Tokenize(line, tokens, "\t "); 
    if (tokens.size() == 0) 
     continue; 

    if (tokens[0].compare("save_application") == 0) 
     SaveApplication(tokens); 

    else if (tokens[0].compare("remove_application") == 0) 
     remove_application(atoi(tokens[1].c_str())); 

    else if (tokens[0].compare("sort") == 0) 
     sort(); 

    else if (tokens[0].compare("print") == 0) 
     print(); 
} 

inf.close(); 
} 

int main(int argc, char **argv) { 
if (argc != 2) { 
    cout << "usage: executable.o command.txt\n"; 
    return 1; 
} 

ExecuteCommands(argv[1]); 
} 

save_application "authors_list1" "title1" "conference1" 2016 "poster" 

save_application "authors_list3" "title3" "conference2" 2010 "oral" 

save_application "authors_list2" "title2" "journal1" 2015 "none" 
print 
sort 
print 
remove_application 0 
print 

を印刷する必要があります:コンパイル時に

"authors_list1" "title1" "conference1" 2016 "poster" 
"authors_list3" "title3" "conference2" 2010 "oral" 
"authors_list2" "title2" "journal1" 2015 "none" 


"authors_list3" "title3" "conference2" 2010 "oral" 
"authors_list2" "title2" "journal1" 2015 "none" 
"authors_list1" "title1" "conference1" 2016 "poster" 


"authors_list2" "title2" "journal1" 2015 "none" 
"authors_list1" "title1" "conference1" 2016 "poster" 

はしかし、それは、これらのエラーを与える:

error: ‘class std::vector<std::vector<std::__cxx11::basic_string<char> > >’ has no member named ‘emplace_back’ database.emplace_back(tokens.begin()+1, tokens.end()); 

error: ‘stoi’ was not declared in this scope int year_of(vector<string> const &record) { return stoi(record[YEAR]); 

Iなぜこれが起こるのだろうと思っています。 このプログラムでは、クラスまたは構造体を使用することはできません。ちょうど私たちに与えられたプログラム。どのようにクラスと構造体がプログラミングを容易にし、実際にクラスなしでプログラミングするのがどれほど難しいかを知ることでした。

+0

私は、コンパイラのための最も一般的な原因は知らないと思うだろう'emplace_back'はC++ 11からのみ追加されたため、' emplace_back'はC++ 11でコンパイルされません。 C++ 11以降でコンパイルしていますか? – Tas

+0

"最新の" C++ 11以降のコンパイラを使用していないか、C++ 11以降を使用するために適切なコンパイラオプションを使用していない可能性があります。 – davidbak

+0

ああ、私はlinux ubuntu 16.04 LTSで端末を使用しています。 –

答えて

0

これは、C++ 11モードを有効にしない(またはコンパイラにはない)ためです。幸いなことに、私はすでにコメントにC++ 03のバージョンを掲載しています。

あなたが間違って編集しました。オンラインコンパイラの美しさは、(この場合には、c++11 was enoughと2小さな微調整がc++03 compatible作る)使用/それをコンパイルするために正確にどのように見ることができるということです - sehe 2 hours ago

+0

はい、ありがとうございます。 –

関連する問題