2012-04-17 77 views
2

我有一個名稱空間定義在一個頭文件中,並在另一個頭文件中使用,但無法找到。具體來說,在名爲「combat/Targetable.hpp」的文件中找不到名爲「players/Players.hpp」中定義的名爲「players」的名稱空間,該名稱爲「combat/Targetable.hpp」無法找到名稱空間,儘管它是正確的

錯誤是

...\source\combat\Targetable.hpp(7): 'players' : is not a class or namespace name 
...\source\combat\Targetable.hpp(7): 'Ownable' : base class undefined 

顯然它是一些我不明白的語法。我花了一些時間簡化代碼,看起來很愚蠢,但忍耐着我。

// source/players/Players.hpp: 
#ifndef PLAYERS_HPP 
#define PLAYERS_HPP 

#include "../Headers.hpp" 

namespace players { 
    class Player{ 

// this class compiles fine. 
// There used to be a "Players.cpp" but it's been simplified away 

    public: 
     int getID(){ return 0; } 
     int getTeam(){ return 0; } 
     string getName(){ return ""; } 
     Vec3 getColor(){ return Vec3(0.0,0.0,0.0); } 
    }; 
} 
#endif 

而且玩家/ Ownable.hpp,這是在同一文件夾中Player.hpp也正常編譯:

// source/players/Ownable.hpp: 
#ifndef OWNABLE_HPP 
#define OWNABLE_HPP 

#include "Players.hpp" 

namespace players { 
    class Ownable; 
    typedef boost::shared_ptr<Ownable> OwnablePTR; 
    typedef boost::weak_ptr<Ownable> OwnableWPTR; 

    class Ownable { 
    public: 
     Ownable(){} 
     Ownable(int playerID) : playerID(playerID){} 
     bool isAlliedWith(OwnablePTR other){ return false; } 

    private: 
     int playerID; 
    }; 
} 

#endif 

這裏的樂趣的開始。我在「source/combat/Targetable.hpp」中有一個文件,它位於與另外兩個目錄不同的目錄中。但是,文件本身似乎包括罰款:

// source/combat/Targetable.hpp: 
#ifndef TARGETABLE_HPP 
#define TARGETABLE_HPP 

#include "../players/Ownable.hpp" 

namespace combat{ 
    class Targetable : public players::Ownable { // ERROR 
    public: 
     Targetable(int playerID){} 
     //Targetable(players::Player player); 

     virtual Vec2 getPosition(){ 
     return Vec2(); 
     } 
     virtual Vec2 getVelocity(){ 
     return Vec2(); 
     } 
    }; 
} 

#endif 

我真的希望這是我錯過了一些愚蠢的語法事情。我甚至試過

using players::Ownable; 

但A)污染,其中包括這一個,和B)不解決任何文件。任何幫助?

編輯:GManNickG明白了,它是一個循環包含在Headers.hpp文件中。謝謝!

+8

'Headers.hpp'包含什麼?我聞到一個通告包括。 – GManNickG 2012-04-17 04:51:40

+0

頭文件包括Camera.hpp,其中包括作戰/ Targetable.hpp。刪除包括似乎修復的東西。不過,我認爲包括衛兵應該處理這個問題。 – whiterook6 2012-04-17 06:47:17

+0

我們能夠幫助您的唯一方法是,如果您發佈*完整*示例,那麼我們可以嘗試編譯它並自己查看錯誤。 – HighCommander4 2012-04-17 06:50:41

回答

3

你有一個通告包括。

首先考慮包括警衛的目的:

// a.hpp 
#ifndef A_HPP 
#define A_HPP 

// stuff 

#endif 

// b.hpp 
#ifndef B_HPP 
#define B_HPP 

#include "a.hpp" 

// stuff 

#endif 

// c.hpp 
#ifndef C_HPP 
#define C_HPP 

#include "a.hpp" 
#include "b.hpp" 

// stuff 

#endif 

// x.cpp 
#include "c.hpp" 

包含c.hpp最終將包含a.hpp兩次。第一次,衛兵沒有定義,一切都好,第二次衛兵防止重新定義。這是我們想要的。

但是,當你有一個循環時,這不起作用。 (它會阻止它,這是好的,但它確實太「太好」了,因爲警衛是在測試後立即定義的,這意味着頭的內容尚未處理)。考慮這個:

// a.hpp 
#ifndef A_HPP 
#define A_HPP 

#include "c.hpp" 

// stuff 

#endif 

// b.hpp 
#ifndef B_HPP 
#define B_HPP 

#include "a.hpp" 

// stuff 

#endif 

// c.hpp 
#ifndef C_HPP 
#define C_HPP 

#include "b.hpp" 

// stuff 

#endif 

// x.cpp 
#include "c.hpp" 

這類似於你有什麼。 x.cpp包括c.hpp,這是第一次被包括在內,所以它定義了C_HPP。然後c.hpp包括b.hpp,其包括a.hpp。然後a.hpp包括c.hpp發現C_HPP已經被定義爲,所以包含什麼都不做。

假設a.hpp仍然設法編譯(即,實際上並不需要c.hpp),然後a.hpp飾面,然後b.hpp飾面,然後c.hpp返回x.cpp之前最後實際上定義其內容。

解決方案是儘量減少您包含的標題數量。使用前向聲明,最重要的是:不要使用「包含所有內容」標頭!這些是可怕的。我懷疑這就是Headers.hpp

1

class Targetable : public ::players::Ownable { . . .怎麼樣?

請注意players之前的全局命名空間限定::

+0

我試過了,沒有去過:''players':'::'左邊的符號必須是「 – whiterook6 2012-04-17 06:45:18

相關問題