JDW
JDW

Reputation: 185

Namespaces and class members

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

Upvotes: 2

Views: 377

Answers (1)

CashCow
CashCow

Reputation: 31435

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.

namespace t {
   namespace graphics {
       class Animation;
   }

   // continue with definitions
}

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.

Upvotes: 2

Related Questions