2010-11-10 48 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