2010-11-10 49 views
2

考慮:命名空間和類成員

#ifndef __t__ENTITY_H 
#define __t__ENTITY_H 

#include "../graphics/animation.h" 

namespace t { 
    namespace entity { 
     namespace type { 
      enum Enum { GFX = 0, SFX, PFX, AI, MAX }; 
     } 

     //template <class T> 
     class Entity { 
      public: 
       Entity(unsigned int, unsigned int, unsigned int); 
       Entity(); 
       ~Entity(); 

       int getPosX() { return m_posX; } 
       int getPosY() { return m_posY; } 
       void setPos(int x, int y) { m_posX = x; m_posY = y; } 
       //TODO: const references 
       unsigned int getGFXId() { return m_ids[type::GFX]; } 
       unsigned int getSFXId() { return m_ids[type::SFX]; } 
       unsigned int getPFXId() { return m_ids[type::PFX]; } 
       int update(const float); 
       int draw(); 
       int fetchGraphics(); 
       int fetchSound(); 
       int fetchPhysics(); 

      protected: 
       //TODO: friend class entity::Handler int reset() 
      private: 
       int updatePhysics(const float); 
       int updateGraphics(const float); 
       int updateSound(const float); 

       int m_posX, m_posY; 
       t::graphics::Animation* m_pAnimation; 
       float m_lastTime; 
       unsigned int m_ids[type::MAX]; 
     }; // class Entity 
     typedef boost::shared_ptr<t::entity::Entity> SPENTITY; 
     typedef boost::shared_ptr<std::vector<SPENTITY> > SPENTITYS; 

    } // namespace entity 
} // namespace t 

#endif // __t__ENTITY_H 

在該代碼中的成員 「T ::圖形::動畫* m_pAnimation;」給「錯誤:'T ::圖形還沒有被宣佈爲」,即使「../graphics/animation.h」的模樣:

#ifndef __t__ANIMATION_H 
#define __t__ANIMATION_H 

#include "frame.h" 
namespace t { 
    namespace graphics { 
     class Animation { 
      public: 
       Animation(); 
       Animation(SPFRAMES); 

       ~Animation(); 

       float getLastFrameChange() { return m_lastFrameChange; } 
       int getCurrentFrameId() { return m_currentFrameId; } 
       //SPFRAME getCurrentFrame() { return m_spFrames.get()[m_currentFrameId]; }//return m_spFrames[m_currentFrameId]; } 
       void setCurrentFrame(int val) { m_currentFrameId = val; } 

       int update(const float); 
       //int fetchDrawables(); 
      protected: 
      private: 
       float m_lastFrameChange; 
       unsigned int m_currentFrameId; 
       unsigned int m_oldFrameId; 
       SPFRAMES m_spFrames; 
     }; // class Animation 
     typedef boost::shared_ptr<Animation> SPANIMATION; 
     typedef boost::shared_ptr<std::vector<SPANIMATION> > SPANIMATIONS; 
    } // namespace graphics 
} // namespace t 

#endif // __t__ANIMATION_H 
+0

urg,代碼格式化已經結束了...... – Nim 2010-11-10 14:03:28

+0

我認爲修復了格式化。 – 2010-11-10 14:15:32

+0

嘗試「graphics :: Animation * m_pAnimation;」因爲你的類已經在「t」名字空間 – Tom 2010-11-10 14:18:49

回答

2

你應該儘量避免頭包括其他頭,除非他們是必要的。在這種情況下,您可以在第一個標題中輸入前向聲明。

namespace t { 
    namespace graphics { 
     class Animation; 
    } 

    // continue with definitions 
} 

有可能您的標題包含在錯誤順序的某處。也許某些動畫包括需要enum,包括你的Entity頭。

我建議將該枚舉移動到另一個頭部。如果它是本地的,則將其放入類範圍中。

順便說一句我也:

  • 搬完的shared_ptr的typedef到剛向前聲明不同的頁眉。任何想要使用這些typedef的人都不一定需要包含類的完整定義,特別是它們在頭文件中使用的位置。

  • 使您的代碼正確無誤。

+0

在向前聲明boost和std內容時存在一些問題(因爲它有時會認爲我的前向聲明是重新刪除......),但解決方案的其他部分工作得很好! – JDW 2010-11-10 15:18:46

+0

我通常會向前聲明的boost聲明,例如你可以做名稱空間boost {template class shared_ptr; }在你的fwd文件中。對於STL,我通常只包含STL頭,儘管對於iostreams,您通常可以包含,例如,您的課程支持流式傳輸。要向前聲明std :: string,你將不得不聲明char_traits和allocator以及basic_string,儘管這是可能的,請參閱http://forums.devx.com/showthread.php?t=89483 – CashCow 2010-11-10 16:04:25