2012-09-03 105 views
0

我對所有的矢量名稱空間和如何正確地返回我的類中的字符串矢量有點困惑。下面是代碼:返回一個字符串矢量

的main.cpp

#include <fstream> 
#include <iostream> 
#include <stdlib.h> 
#include <vector> 
#include <string> 
#include "lab1.h" 

using namespace std; 
readwords wordsinfile; 
words wordslist; 

int main (int argc, char *argv[]) 
{ 
    if (argc != 2) { 
      // Looks like we have no arguments and need do something about it 
      // Lets tell the user 
      cout << "Usage: " << argv[0] <<" <filename>\n"; 
      exit(1); 
    } else { 
      // Yeah we have arguements so lets make sure the file exists and it is readable 
      ifstream ourfile(argv[1]); 
      if (!ourfile.is_open()) { 
        // Then we have a problem opening the file 
        // Lets tell the user and exit 
        cout << "Error: " << argv[0] << " could not open the file. Exiting\n"; 
        exit (1); 
      } 

      // Do we have a ASCII file? 
      if (isasciifile(ourfile)) { 
        cout << "Error: " << argv[0] << " only can handle ASCII or non empty files. Exiting\n"; 
        exit(1); 
      } 

      // Let ensure we are at the start of the file 
      ourfile.seekg (0, ios::beg); 
      // Now lets close it up 
      ourfile.close(); 
    } 

    // Ok looks like we have past our tests 
    // Time to go to work on the file 
    ifstream ourfile2(argv[1]); 
    wordsinfile.getwords(ourfile2); 

lab1.h

#ifndef LAB1_H 
#define LAB1_H 

bool isasciifile(std::istream& file); 

class readwords { 
    public: 
      int countwords(std::istream& file); 
      std::vector<std::string> getwords(std::istream& file); 
}; 

class words { 
    public: 
      void countall(void); 
      void print(void); 
}; 

#endif 

lab1.cpp

#include <fstream> 
#include <iostream> 
#include <map> 
#include "lab1.h" 
#include <vector> 
using std::vector; 
#include <string> 

using namespace std; 

vector<string> readwords::getwords(std::istream& file) { 
    char c; 
    string aword; 
    vector<string> sv; 
    int i = 0; 

        while(file.good()) { 
          c = file.get(); 
          if (isalnum(c)) { 
            if(isupper(c)) { 
              c = (tolower(c)); 
            } 
            if(isspace(c)) { continue; } 
            aword.insert(aword.end(),c); 
          } else { 
            if (aword != "") {sv.push_back(aword);} 
            aword = ""; 
            i++; 
            continue; 
          } 
        } 
    return sv; 
} 

以下是編譯錯誤。

g++ -g -o lab1 -Wall -pedantic main.cpp lab1.cpp 
In file included from lab1.cpp:4:0: 
lab1.h:9:4: error: ‘vector’ in namespace ‘std’ does not name a type 
lab1.cpp:48:54: error: no ‘std::vector<std::basic_string<char> > readwords::getwords(std::istream&)’ member function declared in class ‘readwords’ 
make: *** [lab1] Error 1 

爲什麼我得到這個錯誤,我該如何解決它。感謝您提供任何幫助。

瑞安

+0

現在您可以查看錯誤消息,瞭解問題所在。它告訴你「lab1.cpp」的第4行包含一個名爲「lab1.h」的文件。該文件的第9行正在處理一些尚未定義的稱爲「矢量」的東西。 –

回答

5

你必須在頭文件#include <vector>爲好。實際上,將它包含在標題中就足夠了,因爲包含該標題的所有文件都會隱含地包​​含<vector>

的事情是你的包括順序是:

#include "lab1.h" 
#include <vector> 

因爲你在標題中使用std::vector(包括之前)你的錯誤。反轉包含順序將修復編譯錯誤,但不能解決潛在錯誤 - lab1使用尚未定義的符號。正確的解決辦法是包括<vector>

+0

這工作,但是我以爲你不應該在頭文件中包含#includes。 (我想我在這個想法是錯誤的) – Ryan

+0

@Ryan這是錯誤的,標題應該是自包含的。你應該儘可能使用* include guard *,但就是這樣。你不應該在頭部使用''使用'指令,這是真的。 –

+0

謝謝你爲我清理。 – Ryan

1

編譯器按寫入的順序查看代碼。這也適用於#include指令:文件的內容被視爲已被寫入#include的文件中。正如@LuchianGrigore提到,最好的解決辦法是增加

#include <vector> 

爲 「lab1.h」。但是你可以通過在「lab1.cpp」中移動#include <vector>來隱藏這個問題,這樣它在#include "lab1.h". That would make the error go away, because the compiler would have already read`之前開始讀取「lab1.h」。這不是你應該做的,但是這種事情可能會偶然發生並隱藏實際問題。