2013-10-18 29 views
0

所以我決定嘗試使用我在前一個問題中只使用向量類寫入的程序,但我仍然遇到錯誤。錯誤與向量和結構

的錯誤:

error: 'match' does not name a type 
error: 'match' was not declared in the scope 
error: 'conversion from 'int' to non-scalar type 'WordInfo' requested 

我的代碼:

#include <cstdlib> 
    #include <iostream> 
    #include <vector> 
    #include <algorithm> 


    using namespace std; 

    struct WordInfo { 
     string text; 
     int count; 
    }; 

    int main(int argc, char** argv) { 

     enum { 
      total, unique, individual 
     } mode = total; 
     for (int c; (c = getopt(argc, argv, "tui")) != -1;) { 
      switch (c) { 
       case 't': mode = total; 
        break; 
       case 'u': mode = unique; 
        break; 
       case 'i': mode = individual; 
        break; 

      } 
     } 
     argc -= optind; 
     argv += optind; 
     string word; 
     vector<string> list; 
     vector<WordInfo> words; 
     int count = 0; 
     while (cin >> word) { 

      switch(mode){ 
       case total : 
        count += 1; 
        break; 
       case unique : 
        if (find(list.begin(), list.end(), word) != list.end()){ 
        } else { 
         count += 1; 
        } 
        break; 
       case individual : 
        if (find(list.begin(), list.end(), word) != list.end()) { 
         auto match = find(list.begin(), list.end(), word); 
         words.at(match - list.begin()).count++; 
        } else { 
         int count = 1; 
         WordInfo tmp = (word, i); 
         words.push_back(tmp); 
        } 
        }  
     } 


     switch (mode) { 
      case total: cout << "Total " << count << endl; 
       break; 
      case unique: cout << "Unique " << count << endl; 
       break; 
      case individual: 
       for (int i = 0; i < words.size(); i++){ 
        cout << words.at(i); << endl; 
       } 
       break; 
     } 

     return 0; 
     } 

任何及所有的幫助,將不勝感激,謝謝。

+0

'WordInfo tmp =(word,i);'不會做你認爲它做的事。查找逗號運算符。 – chris

回答

2

您確定您使用的是C++ 11嗎?

auto與早期編譯器有完全不同的含義。

如果您使用g++,嘗試用-std=c++11標誌編譯:

g++ -std=c++11 foobar.cpp 
0

如果你使用G ++,設置-std = C++ 11儘量使用自動的讓編譯器圖基於表達式的類型。然而,你可能也想清理你的代碼 - 你不應該在if語句中找到一次,然後再次在裏面。

0

得到了一些語法錯誤:

test.cpp:54:44: error: use of undeclared identifier 'i' 
       WordInfo tmp = (word, i); 
            ^

test.cpp:68:31: error: invalid operands to binary expression ('ostream' (aka 

'basic_ostream<char>') and 'WordInfo') 

        cout << words.at(i); << endl; 
             ^

看,你使用未定義的變量i和一個額外的;在cout聲明中。