命名空间和类成员
考虑:
#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::graphics::Animation* m_pAnimation;”给出“错误:'t::graphics'尚未声明”,即使“../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
Consider:
#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
In that code the member "t::graphics::Animation* m_pAnimation;" gives "error: 't::graphics' has not been declared", even though "../graphics/animation.h" looks like:
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该尽量避免使标头包含其他标头,除非有必要。在这种情况下,您可以在第一个标头中放入前向声明。
您的标头可能以错误的顺序包含在某个位置。也许动画包含的内容需要枚举,因此包含您的实体标头。
我建议将该枚举移至另一个标头。如果它是类的本地变量,则将其放入类范围内。
顺便说一句,我还会:
将这些shared_ptr typedef 移动到仅具有前向声明的不同标头。任何想要使用这些 typedef 的人都不一定需要包含类的完整定义,特别是在头文件中使用它们的地方。
使您的代码常量正确。
You should try to avoid making headers include other headers unless they are necessary. In this case you can put in a forward declaration in the first header.
There is a possibility that your headers are being included somewhere in the wrong order. Maybe something Animation is including needs that enum so is including your Entity header.
I would suggest moving that enum to another header. If it is local to the class put it in the class scope.
As an aside I would also:
Move those shared_ptr typedefs to a different header that has just forward declarations. Anyone wanting to use those typedef's would then not necessarily need to include the full definition of the class, particulary where they are used in a header file.
Make your code const-correct.