Thursday, 15 July 2010

c++ - Circular dependency confusion -



c++ - Circular dependency confusion -

i read disch article here in c++ forum headers http://www.cplusplus.com/forum/articles/10627/

i have code here somehow confusing

resourceholder.h

using namespace std; template<typename resource, typename identifier> class resourceholder{ public: void load(identifier id, const std::string &filename); template<typename parameter> void load(identifier id, const std::string &filename, const parameter &secondparam); resource &get(identifier id); const resource &get(identifier id) const; private: void insertresource(identifier id, std::unique_ptr<resource> resource); private: std::map<identifier, std::unique_ptr<resource>> mresourcemap; };

resourceidentifier.h

// forwards declaration of sfml classes namespace sf { class texture; } namespace textures { enum id { eagle, raptor, desert, }; } // forwards declaration , few type definitions template <typename resource, typename identifier> class resourceholder; typedef resourceholder<sf::texture, textures::id> textureholder;

aircraft.h

#pragma 1 time #include "entity.h" #include "resourceidentifiers.h" class aircraft : public entity { public: enum type{ eagle, raptor }; aircraft(type type, const textureholder& textures); private: virtual void drawcurrent(sf::rendertarget &target, sf::renderstates states) const; private: type mtype; sf::sprite msprite; };

i believe using right way , wrong way base of operations on article read.

aircraft.h

include

resourceidentifier.h

which contains forwards declaration of

resourceholder.h

stated in article need utilize pointer class or reference class if going forwards declare.

in code utilize forwards declaration never utilize pointer class. template class

thing when got error on aircraft.cpp incompatible type

aircraft.cpp

#include "aircraft.h" #include "resourceholder.h" // --------- got error if not included. textures::id totextureid(aircraft::type type){ switch (type) { case aircraft::type::eagle: homecoming textures::id::eagle; case aircraft::type::raptor: homecoming textures::id::raptor; } homecoming textures::id::eagle; } void aircraft::drawcurrent(sf::rendertarget &target, sf::renderstates states) const{ target.draw(msprite, states); } aircraft::aircraft(type type, const textureholder &textures) : mtype(type), msprite(textures.get(totextureid(type))) //----------> error if resourceholder.h not included. error: incompatible type { }

the error:

incompatible type.

but confusing. included resourceidentifier.h has class forwards declaration of resourceholder.h , in aircraft.h .

why error? linked together. resourceholder has own class declaration in diferent file did not included here. every class has declaration.

also texture reference template class on resourceidentifier.h. why have declare resourceholder.h 1 time again on aircraft.cpp? class fowarded on resourceidentifier.h. didnt utilize template class object. reference.

c++

No comments:

Post a Comment