SFML Game Development book example game

Jan 30, 2014 at 8:49pm
Hi everyone

In game.h
ResourceHolder<Textures::ID, sf::Texture> mTextures;

I keep this file and ResourceHolder is a class which mainly focused on keeping the data of any resource such as sound, music and texture i guess:D. It is from SFML Game Development book.

And i use it in game.cpp like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Game::Game()
    :   mWindow(sf::VideoMode(640, 480), "SFML Application")
    ,   mTimePerFrame(sf::seconds(1.f / 60.f))
    ,   mTextures()
    ,   mPlayer()
    ,   mIsMovingLeft(false)
    ,   mIsMovingRight(false)
    ,   mIsMovingDown(false)
    ,   mIsMovingUp(false)
{
    mTextures.load(Textures::Airplane, "Media/Textures/Eagle.png");
    mTextures.load(Textures::Landscape, "Media/Textures/Landscape.png");
    mPlayer.setTexture(mTextures.get(Textures::Airplane));
    mPlayer.setPosition(100.f, 100.f);
}


And it gives me linker errors such as
D:\Projeler\sfmlSpaceShooter\classes\game.cpp:13: error: undefined reference to `ResourceHolder<Textures::ID, sf::Texture>::load(Textures::ID, std::string const&)'
.
.


Ant ResourceHolder class is like:
1
2
3
4
5
6
7
8
9
10
11
template <typename Identifier, typename Resource>
class ResourceHolder
{
private:
    std::map<Identifier, std::unique_ptr<Resource>> mResourceMap;

public:
    void load(Identifier id, const std::string& filename);
    Resource& get(Identifier id);
    const Resource& get(Identifier id) const;
};


And the other code definitions goes the same way like this example one
1
2
3
4
5
6
7
8
9
10
template <typename Identifier, typename Resource>
void ResourceHolder<Identifier, Resource>::load(Identifier id, const std::string& filename)
{
    std::unique_ptr<Resource> resource(new Resource());
    if(!resource->loadFromFile(filename))
        throw std::runtime_error("ResourceHolder::load - failed to load " + filename);

    auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource)));
    assert(inserted.second);
}


Like i said i am working from SFML Game Development book but it does not have its example codes as it teaches :(

What can i do to solve it.

Thanks in advance to all of you and sorry for this long message
Last edited on Jan 30, 2014 at 8:50pm
Jan 30, 2014 at 8:55pm
Depending on your compiler you may have to put template function definitions in the same header file.
Jan 30, 2014 at 8:58pm
Jan 31, 2014 at 4:18pm
Thanks for the reply and answers. Problem solved.
Jan 31, 2014 at 5:40pm
Like i said i am working from SFML Game Development book but it does not have its example codes as it teaches :(


https://github.com/LaurentGomila/SFML-Game-Development-Book

There will be some minor differences from the code in the book as the code has been improved with reader input since publishing.
Topic archived. No new replies allowed.