2012-09-13 75 views
0

好吧,所以我是一個HTML/Javascript/PHP專業人士,但我正在嘗試學習C++。我仍然是一個新手,我有一個C++編程項目,我有錯誤。C++試圖使用#ifndef和#include語句

包含int main()的文件是'football.cpp',並且我必須使用三個類的.h和.cpp文件:Game,Team和Date。團隊的每個實例都包含一個遊戲矢量,每個遊戲都包含一個日期。日期不包含任何其他類的實例。

我想找到一種方法在文件頂部使用#include和#ifndef語句,以便在編譯時不會出錯,但是我沒有找到可用的組合。我不確定是否還有其他錯誤。這是我目前的#include節,還不算其他庫:

football.cpp

#include "game.h" 
#include "team.h" 
#include "date.h" 

team.h

#ifndef __game_h_ 
#define __game_h_ 
#endif 

team.cpp

#include "team.h" 
#ifndef __game_h_ 
#define __game_h_ 
#endif 

game.h

#ifndef __date_h_ 
#define __date_h_ 
#endif 

game.cpp

#include "game.h" 
#ifndef __date_h_ 
#define __date_h_ 
#endif 

date.cpp

#include "date.h" 

我使用Cygwin克++編譯器,我用編譯它的行是:

g++ football.cpp team.cpp game.cpp date.cpp -o football.exe 

這裏是所有我得到的錯誤:(警告,牆壁文字)

$ g++ football.cpp team.cpp game.cpp date.cpp -o football.exe 

In file included from football.cpp:9:0: 
game.h:15:96: error: ‘Date’ has not been declared 
game.h:24:1: error: ‘Date’ does not name a type 
game.h:37:1: error: ‘Date’ does not name a type 
football.cpp: In function ‘int main(int, char*)’: 
football.cpp:65:70: error: no matching function for call to ‘Game::Game(std::string&, std::string&, int [5], int [5], Date&)’ 
game.h:15:1: note: candidates are: Game::Game(std::string, std::string, const int, const int*, int) 
game.h:14:1: note: Game::Game() 
game.h:10:12: note: Game::Game(const Game&) 
In file included from team.cpp:4:0: 
team.h:24:8: error: ‘Game’ was not declared in this scope 
team.h:24:12: error: template argument 1 is invalid 
team.h:24:12: error: template argument 2 is invalid 
team.cpp: In member function ‘float Team::getStat3()’: 
team.cpp:36:26: error: request for member ‘size’ in ‘((Team*)this)->Team::games’, which is of non-class type ‘int’ 
team.cpp:37:21: error: invalid types ‘int[int]’ for array subscript 
team.cpp:37:50: error: invalid types ‘int[int]’ for array subscript 
team.cpp:38:21: error: invalid types ‘int[int]’ for array subscript 
team.cpp:38:47: error: invalid types ‘int[int]’ for array subscript 
team.cpp:38:76: error: invalid types ‘int[int]’ for array subscript 
team.cpp:38:106: error: invalid types ‘int[int]’ for array subscript 
team.cpp: In function ‘bool compare2(Team, Team)’: 
team.cpp:45:39: error: request for member ‘size’ in ‘t1.Team::games’, which is of non-class type ‘const int’ 
team.cpp:46:39: error: request for member ‘size’ in ‘t2.Team::games’, which is of non-class type ‘const int’ 
team.cpp:50:17: error: request for member ‘size’ in ‘t1.Team::games’, which is of non-class type ‘const int’ 
team.cpp:50:35: error: request for member ‘size’ in ‘t2.Team::games’, which is of non-class type ‘const int’ 
team.cpp:52:24: error: request for member ‘size’ in ‘t1.Team::games’, which is of non-class type ‘const int’ 
team.cpp:52:43: error: request for member ‘size’ in ‘t2.Team::games’, which is of non-class type ‘const int’ 
team.cpp: In function ‘bool compare3(Team, Team)’: 
team.cpp:62:29: error: passing ‘const Team’ as ‘this’ argument of ‘float Team::getStat3()’ discards qualifiers 
team.cpp:63:29: error: passing ‘const Team’ as ‘this’ argument of ‘float Team::getStat3()’ discards qualifiers 
In file included from game.cpp:5:0: 
game.h:15:96: error: ‘Date’ has not been declared 
game.h:24:1: error: ‘Date’ does not name a type 
game.h:37:1: error: ‘Date’ does not name a type 
game.cpp: In constructor ‘Game::Game()’: 
game.cpp:26:3: error: ‘date’ was not declared in this scope 
game.cpp:26:15: error: ‘Date’ was not declared in this scope 
game.cpp: At global scope: 
game.cpp:29:94: error: ‘Date’ has not been declared 
game.cpp:29:1: error: prototype for ‘Game::Game(std::string, std::string, int*, int*, int)’ does not match any in class ‘Game’ 
game.h:10:12: error: candidates are: Game::Game(const Game&) 
game.h:15:1: error: Game::Game(std::string, std::string, const int*, const int*, int) 
game.cpp:13:1: error: Game::Game() 
game.cpp: In member function ‘int Game::getVisitingScore(int) const’: 
game.cpp:80:10: error: ‘visitingScores’ was not declared in this scope 
game.cpp: At global scope: 
game.cpp:94:1: error: ‘Date’ does not name a type 


如果需要進一步澄清,我會編輯,但我認爲不會。

任何幫助將被廣泛讚賞。

+0

只要你知道,雙下劃線在任何範圍內保留,並用下劃線開始在全球範圍內保留(而把一個大寫字母之後,在任何範圍內保留第一個下劃線)。 – chris

+0

請勿使用以(或包含)兩個下劃線開頭的名稱(或以一個下劃線後跟大寫字母開頭的名稱)。他們留給執行。但這與問題無關。 –

+0

@PeteBecker,它花了我100次的時間來閱讀注意到的答案,但是雙重下劃線不一定要在開始時違反規則。 – chris

回答

5

我認爲你誤解使用包括警衛

#ifndef __game_h_ 
#define __game_h_ 
// ... 
#endif 

的一組語句上面通常僅在頭文件中描述你的遊戲接口一起使用,以防止多包容這個頭文件。

但在你的代碼,您已經添加了包括警衛頭實施,你也似乎是混亂的實體 - 除非我誤解你的文件的內容 - 例如在team.h,你有什麼似乎是包括後衛遊戲

我的猜測是,你濫用包括守衛實際上是防止某些類型被定義。

正如其他人所提到的,不要使用以雙下劃線開頭的名稱。

舉個例子,添加包括警衛您團隊頭,以防止多包含該文件的,並給予包括警衛反映,他們正在守衛該模塊的名稱:

// team.h 
#ifndef TEAM_H 
#define TEAM_H 

// ... code for team.h in here 

#endif // TEAM_H 
+0

'_TEAM_H_'也是保留的,因爲它以下劃線和大寫字母開頭。真的不需要在這樣的地方撒下下劃線; 「TEAM_H」怎麼樣? –

+0

@MikeSeymour謝謝 – pb2q

+0

值得一提的是,每個實現文件都是單獨編譯的,並且include guard只防止在單個編譯中包含多個包含。我們必須每週從一位新手那裏得到一個問題,他認爲包括警衛在內的所有編輯工作中都會阻止多重內容。 – john

0

你不需要衛兵在C++文件

0

該錯誤是,你應該使用包括警衛僅在頭文件

如果你有

BaseClass.h 
struct HumanEntity { } 

,然後有不同的類使用該基類,如果沒有包含警衛,可能會遇到問題。這就是爲什麼你把一個

1

它看起來像你在這裏有兩個問題:第一個是你在源文件中使用包括守衛;第二個是你包含文件的順序是錯誤的。

首先,包括警衛。其他人已經回答了這個問題,所以我會簡短介紹一下。包含守護程序僅用於頭文件中,以防止多次聲明類/類型。例如,如果我有:

「game.h」

class Game {}; 

「game.cpp」

#include "game.h" 
#include "game.h" // ERROR: class Game is declared twice. 

爲了解決這個問題,我會用包括衛兵「 game.h「:

#ifndef GAME_H_INCLUDED 
#define GAME_H_INCLUDED 

class Game {}; 

#endif 

第一次th e文件被包含,GAME_H_INCLUDED沒有被定義,所以Game被聲明。第二次包含,GAME_H_INCLUDED 定義爲,所以聲明被跳過。

你的問題是,你包括你的源文件守衛會導致所有的執行被跳過:

破「game.cpp」

#include "game.h" 
#ifndef GAME_H_INCLUDED // This is defined in "game.h" so everything will be 
         // skipped until #endif is encountered. 
#define GAME_H_INCLUDED 

// Implementation of Game class 

#endif 

二,包含標題的順序不正確。我猜你有這樣的事情:

「game.h」

#ifndef GAME 
#define GAME 

class Game 
{ 
    Date mDate; // Member of type Date, declared in "date.h" 
}; 

#endif 

「date.h」

#ifndef GAME 
#define GAME 

class Date 
{ 
}; 

#endif 

「的遊戲。CPP 「

#include "game.h" // ERROR: Game relies on the Date class, 
        //   which isn't included yet 
#include "date.h" 

爲了解決這個問題要麼調換順序中包括 」game.cpp「:

」game.cpp「

#include "date.h" 
#include "game.h" // Fine, we know what a Date is now. 

或包含」 日期。 h「in」game.h「:

」game.h「

#include "date.h" 
#ifndef GAME 
#define GAME 

class Game 
{ 
    Date mDate; // Member of type Date, declared in "date.h" 
}; 

#endif 

「game.cpp」

#include "game.h" // Still fine, "date.h" is included by "game.h" 
+0

+1格式良好的答案 –