2013-03-11 53 views
0

我對C++相當陌生,&在將向量聲明爲類變量時遇到問題。我已經通過使用類似的策略讓他們在我的代碼中的其他地方工作,但它不喜歡我的頭文件。'vector'不會命名一個類型

error: ‘vector’ does not name a type 
error: ‘vector’ has not been declared 
error: expected ‘,’ or ‘...’ before ‘<’ token 
error: ‘vector’ does not name a type 

我已經評論了GCC指出的問題。

#ifndef HEADER_H 
#define HEADER_H 

#include <cstdlib> 
#include <vector> 
#include <string> 

using std::string; 

// Class declarations 

class Node { 
    int id; 
    string type; 
public: 
    Node(int, string); 
    int get_id(); 
    string get_type(); 
    string print(); 
}; 

class Event { 
    string name, date, time; 
public: 
    Event(string, string, string); 
    string get_name(); 
    string get_date(); 
    string get_time(); 
    string print(); 
}; 

class Course { 
    char id; 
    std::vector<Node*> nodes[40];  // This one 
public: 
    Course(char, std::vector<Node*>); // This one 
    char get_id(); 
    std::vector<Node*> get_nodes(); // & this one. 
    string print(); 
}; 


class Entrant { 
     int id; 
     Course* course; 
     string name; 
    public: 
     Entrant(int, char, string); 
     int get_id(); 
     Course* get_course(); 
     string get_name(); 
     string print(); 
    }; 

    // Function declarations 

void menu_main(); 

void nodes_load(); 
void event_create(); 
void entrant_create(); 
void course_create(); 


#endif /* HEADER_H */ 

在我的IDE錯誤Here's a screenshot,如果提供任何更多的線索。

+2

http://liveworkspace.org/code/40cuIA$1一切正常...只有一個關於未知類型的課程(前向聲明修復它)的錯誤。 – ForEveR 2013-03-11 10:39:21

+1

你是否試圖單獨在乾淨的主要方法中包含這個頭文件?我最好的猜測是,這是由包裝器造成的,而不是包含器本身造成的。 – daramarak 2013-03-11 10:41:58

+0

我試過把它包含在一個乾淨的主文件中 - 由Netbeans生成 - 我仍然得到相同的錯誤。 – gideonparanoid 2013-03-11 10:52:23

回答

3

我從實際編譯代碼中看到的唯一問題是,您在Entrant類中使用Course,但在此時您沒有Course的定義。

如果向前聲明Course略高於Entrant像這樣:

class Course; 

class Entrant { }; //class definition 

那麼你的代碼編譯,每本live example

+0

解決了這個問題,雖然它不是錯誤的過程,但它是一個有用的提示,歡呼聲。 – gideonparanoid 2013-03-11 10:45:25

+0

@Psygnosys很好,很難用代碼發佈來解決某些問題,但不會重現實際問題。 – 2013-03-11 10:46:03

+0

是的,我不明白爲什麼這不會重現這個問題,因爲錯誤信息指向這個頭文件,&那些行:S – gideonparanoid 2013-03-11 10:50:59

0

已經安裝了STL?也許這會幫助你http://ubuntuforums.org/showthread.php?t=1261897

+1

如果有人設法在沒有'std的情況下安裝一個C++編譯器,我會感到驚訝'圖書館! – 2013-03-11 10:44:12

+0

我有Ubuntu 12.10,如果它沒有它,我會感到驚訝。無論如何,我都遵循了指南,因爲它沒有指定文件夾。沒有什麼改變。 – gideonparanoid 2013-03-11 10:53:16

+1

該鏈接反映了由於使用「STL」引用C++標準庫而引起的根本性誤解。如果您使用標準庫對象和函數,則不需要附加組件。 'std :: vector'是C++標準庫的一部分。 – 2013-03-11 14:05:32

3

你在作弊;-)。您提供給我們的代碼有std::vector,它可以正常工作,而截圖中的代碼有vector,這不起作用(編譯器不知道從哪裏得到它)。

解決方案:將您的代碼更改爲使用std::vector

+0

另一種可能的解決方案是使用namespace std添加 ; 代碼。 – 2013-03-12 09:21:00

+0

@EugeneB這不是一個好的解決方案。由此產生的命名空間污染*將會使你遲早會陷入困境。 – us2012 2013-03-12 09:34:13

+0

我從來沒有認爲自己是專家,儘管我認爲對於簡單的程序來說,這比攜帶std :: everywhere更好。雖然,這只是我的看法。這可能只是一種風格和習慣問題。 – 2013-03-12 14:00:16