Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Game Development book problem ( chapter 3 )  (Read 6912 times)

0 Members and 2 Guests are viewing this topic.

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
SFML Game Development book problem ( chapter 3 )
« on: August 22, 2014, 03:54:44 pm »
Hi everybody,
I'm currently working on this great book, and i'm having trouble at chapter 3.
Here is the error message :
C:\...\SFML_game_development\src\Aircraft.cpp|16|undefined reference to `ResourceHolder<sf::Texture, Textures::ID>::get(Textures::ID) const'|

I could give you the code but i don't think that's more useful, I also tried with the official code and it still doesn't work...I heard this could be because of my compiler and it's a template issue but i don't know how to fix that at the moment.

Here is my Aircraft.cpp where the error is detected :
Quote
#include "Aircraft.h"

#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderStates.hpp>


Textures::ID toTextureID(Aircraft::Type type){
    switch(type){
        case Aircraft::Eagle:
            return Textures::Eagle;
        case Aircraft::Raptor:
            return Textures::Raptor;
    }
}

Aircraft::Aircraft(Type type, const TextureHolder& textures) : mType(type), mSprite(textures.get(toTextureID(type)))
{
    sf::FloatRect bounds = mSprite.getLocalBounds();
    mSprite.setOrigin(bounds.width/2.f, bounds.height/2.f);
}


My header for Aircraft class :

Quote
#include <Entity.h>
#include <SFML/Graphics/Sprite.hpp>
#include "ResourceHolder.h"

namespace Textures
{
   enum ID
   {
      Eagle,
      Raptor,
      Desert,
   };
}

template <typename Resource, typename Identifier>
class ResourceHolder;

typedef ResourceHolder<sf::Texture, Textures::ID> TextureHolder;

class Aircraft : public Entity
{
    public:
        enum Type
        {
            Eagle,
            Raptor,
        };
    public:
        Aircraft(Type type, const TextureHolder& textures);
        virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
    protected:
    private:
        Type mType;
        sf::Sprite mSprite;
};

The ResourceHolder.h and ResourceHolder.cpp are the exact same as in https://github.com/SFML/SFML-Game-Development-Book/tree/master/03_World

(but as i said, i also tried to c/p the original code and still doesn't work)

Thanks a lot and sorry for annoying you guys

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #1 on: August 22, 2014, 04:08:50 pm »
First you should use the code tag for sourcecode not a quote so it will be easier to read.

Then we will need a bit more information about your setup:
Compiler, OS.

And check if your ResourceHolder.inl contains the "ResourceHolder<sf::Texture, Textures::ID>::get(Textures::ID) const" (There should be one get without const and one with)



AlexAUT

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #2 on: August 22, 2014, 05:23:53 pm »
Hey, thanks for your replay.
Sorry about the quote block, i didn't find the code tag when I posted it.
Here is my ResourceHolder.inl interseting part :

template <typename Resource, typename Identifier>
Resource& ResourceHolder<Resource, Identifier>::get(Identifier id)
{
        auto found = mResourceMap.find(id);
        assert(found != mResourceMap.end());

        return *found->second;
}

template <typename Resource, typename Identifier>
const Resource& ResourceHolder<Resource, Identifier>::get(Identifier id) const
{
        auto found = mResourceMap.find(id);
        assert(found != mResourceMap.end());

        return *found->second;
}

I'm using Code::Blocks, GNU GCC Compiler and Window 8

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: SFML Game Development book problem ( chapter 3 )
« Reply #3 on: August 22, 2014, 05:42:00 pm »
I see a bit of C++11 stuff in there, so; what compiler version?

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #4 on: August 22, 2014, 06:11:47 pm »
I don't think it is a C++ 11 issue, I'm using it and had no errors with it until I update the aircraft method.
http://puu.sh/b3jfe/bfadd3bd74.png

Where do I find my compiler version?

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: SFML Game Development book problem ( chapter 3 )
« Reply #5 on: August 22, 2014, 07:27:46 pm »
The way I've done it on Windows is run the mingwvars.bat file in your compiler's directory, then open up a cmd, and run g++ -v.

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #6 on: August 22, 2014, 07:40:55 pm »
Thanks for your answer, here is a screenshot from what i get :
http://puu.sh/b3oVY/ca36960a43.png

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: SFML Game Development book problem ( chapter 3 )
« Reply #7 on: August 22, 2014, 10:32:11 pm »
IIRC, gcc 4.7.1 doesn't have full C++11 support, I'd recommend downloading a newer version of gcc. I personally recommend from here: http://tdm-gcc.tdragon.net/download

There is an installer option if you're not sure what to download, how to setup, etc.

I can't remember if Code::Blocks auto detects installed compilers (I use CodeLite), so you may need to setup C::B to use the new compiler.

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #8 on: August 22, 2014, 10:51:19 pm »
Thanks, I'll try that right now

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #9 on: August 22, 2014, 11:15:04 pm »
Still doesn't work. :/
Someone had a similar error here : http://www.cplusplus.com/forum/general/122601/
I don't know how he solved it

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: SFML Game Development book problem ( chapter 3 )
« Reply #10 on: August 22, 2014, 11:24:04 pm »
Do you not have to inline those functions?

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #11 on: August 22, 2014, 11:51:55 pm »
How does that works?

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: SFML Game Development book problem ( chapter 3 )
« Reply #12 on: August 23, 2014, 06:06:30 am »
Example with the code you gave:
template <typename Resource, typename Identifier>
inline Resource& ResourceHolder<Resource, Identifier>::get(Identifier id)
{
    auto found = mResourceMap.find(id);
    assert(found != mResourceMap.end());

    return *found->second;
}

template <typename Resource, typename Identifier>
inline const Resource& ResourceHolder<Resource, Identifier>::get(Identifier id) const
{
    auto found = mResourceMap.find(id);
    assert(found != mResourceMap.end());

    return *found->second;
}
 

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #13 on: August 23, 2014, 12:46:58 pm »
Thank you but that doesn't change anything, I'm still having the same error :/ :'(

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: SFML Game Development book problem ( chapter 3 )
« Reply #14 on: August 23, 2014, 01:06:13 pm »
Try to copy the content of the resourceholder.inl to the bottom of resourceholder.hpp.

//HPP
template<...>
class ResourceHolder
{
public:
    //... Declaration here
}
//Definition here
...
 


AlexAUT