2014-01-16 77 views
0

我有我有兩個文件,這是嚴格的定義,全局數據類型定義等等C++從頭命名的typedef夾雜找不到類型

gamedata.h

#include <utility> 
#include "player.h" 

namespace GameData { 
    enum Color { 
     BLACK = 0, 
     WHITE = 1 
    }; 
    typedef typename std::pair<char, int> Position; 
    typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move; 
    typedef typename std::pair<Player*, Player*> Players; 
}; 

問題:

(1)播放器類中找不到 - 即使我用頭

#include <string> 
#include "board.h" 
#include "gamedata.h" 

class Player { 
public: 
    virtual ~Player() {} 
    virtual Move getMove(Board &c) = 0; 
}; 
包括它

(2)在播放器標題中的Move getMove功能無法解決移動

`Move` does not name a type 

我最好的猜測是,這種情況正在發生,因爲它是一個圓形的包括其中兩個需要包括對方。

所以我向前gamedata.h

class Player; 
namespace GameData { 
    enum Color { 
     BLACK = 0, 
     WHITE = 1 
    }; 
    typedef typename std::pair<char, int> Position; 
    typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move; 
    typedef typename std::pair<Player*, Player*> Players; 
}; 

,修正了播放器問題聲明的球員,但現在的舉動仍是靠不住的。

// None work 
GameData::Move 
Move 

代碼:

http://pastebin.com/i0GJz6xt

編輯1

我想改變這種:

typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move; 

typedef typename std::pair<Position, Position> Move; 

但我認爲這是產生錯誤,所以我沒有恢復它。

+0

不使用模板時,不要使用類型名稱。 http://stackoverflow.com/questions/7923369/when-is-the-typename-keyword-necessary –

+0

注意到謝謝。 – user2997491

+0

你應該使用包括守衛。 #ifndef PLAYER_H然後#define PLAYER_H – TheDude

回答

1

好吧我找到了50個搜索後的答案哈哈。當我說我試圖轉發申報class Player我沒有刪除#include "player.h"這是問題。

從彼得·87報價:

你有一個循環依賴。 janitor.h包含lunch.h。 lunch.h包含janitor.h,但是頭文件可以阻止janitor.h被包含,並且lunch.h失敗,因爲它取決於janitor.h中的內容。

爲防止出現此問題,您可以使用前向聲明。

而不是#include「janitor.h」你寫類管理員;只要你擁有的是指向或看管理員對象的引用,這就會工作。如果您使用該對象,則需要在源文件中包含標題,這樣您可能必須包含它們。對所有其他標題做同樣的事情,而不僅僅是janitor.h。一個很好的規則是使用前向聲明,如果它足夠的話,只有在必須的時候才包含。

來源:http://www.cplusplus.com/forum/general/56475/

+1

我以爲你在你原來的問題中說過你試過這個,但它仍然不起作用?你能否解釋一下Peter87的評論,以便我能理解他建議的內容和你最初嘗試的內容之間的區別?謝謝:) – starsplusplus

+0

不要忘記標記爲已關閉。 – krish

+0

@starsplusplus將更新答案。 – user2997491

相關問題