I read Disch article here in c++ forum about headers
http://www.cplusplus.com/forum/articles/10627/
I have a code here that is somehow confusing
ResourceHolder.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// Forward declaration of SFML classes
namespace sf
{
class Texture;
}
namespace Textures
{
enum ID
{
Eagle,
Raptor,
Desert,
};
}
// Forward declaration and a few type definitions
template <typename Resource, typename Identifier>
class ResourceHolder;
typedef ResourceHolder<sf::Texture, Textures::ID> TextureHolder;
|
Aircraft.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#pragma once
#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 this is using the right way and the wrong way base on the article i read.
include
which contains a forward declaration of
Stated in the article that you need to use pointer to class or reference to class if you are going to forward declare.
In my code they use forward declaration but never use pointer to class. Its actually a template class
Thing is that when I got an error on my Aircraft.cpp about incompatible type
Aircraft.cpp
1 2 3 4 5 6
|
#include "Aircraft.h"
#include "ResourceHolder.h" // --------- I got an error if this is not included.
Aircraft::Aircraft(Type type, const TextureHolder &textures) : mType(type), mSprite(textures.get(toTextureID(type))) //----------> this is where i get an error if ResourceHolder.h is not included
{
}
|
But this is confusing. I included the
that has a class forward declaration of
, in my
.
Why do i get that error? Everything is linked together. ResourceHolder also has its own class declaration in diferent file which i did not included here. But every class has a declaration.
Also texture is a reference to TextureHolder that is on ResourceIdentifier.h. Why do i have to declare the ResourceHolder.h again?