SFML community forums

Help => Graphics => Topic started by: mvl on September 25, 2013, 07:46:48 pm

Title: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: mvl on September 25, 2013, 07:46:48 pm
Hello I am marnicq and I was following along with the book of sfml game development.

Which by the way is great but I have a little problem in chapter 2 keeping track of your textures recourse management I am stuck at page 43 with the code:
std::unique_ptr<sf::Texture> texture(new sf::Texture());

I get an error: undefined reference to `sf::Texture::Texture()'|

I did include sfml graphics in the haeder plaese help I dont see wats wrong with the code
btw: if i use a normal pointer like this:
sf::Texture* texture(new sf::Texture())
it does work!
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: Nexus on September 25, 2013, 08:56:55 pm
Which compiler? Does it support C++11? With g++, you have to specify the -std=c++0x or -std=c++11 flag.

Generally, "undefined reference" is a linker error, which is strange when you say the raw pointer works, but std::unique_ptr doesn't. Are you sure there is only this difference? When setting up, did you follow the ReadMe.txt from the Git repository (https://github.com/SFML/SFML-Game-Development-Book)?
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: mvl on September 25, 2013, 09:13:17 pm
yes I did read the readme and the example code compiles (strangely enough) but the error was when I tried to follow along with the book.

it is the standard compiler of code blocks and I already had an error from the std::unique_ptr but fixed it with chekking
have g++ follow the comming c++ox iso ...
have g++ follow the c++11 ...
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: mvl on September 26, 2013, 04:57:51 pm
eu there are no reactions any more in I was thinking you were thinking i already solved it but i didn't I said solved a previous problem with smart pointers that way?!...
Title: AW: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: eXpl0it3r on September 26, 2013, 07:10:34 pm
Show a complete and minimal example that reproduces the error.
It's hard to tell from a single line what could be wrong.

Also we still don't know which compiler you're using.
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: mvl on September 26, 2013, 09:54:21 pm
The source code is almost exactly copied from the book with the exception of a .h file for the enum of the textures
but here is the class anyway

the TextureHolder.h

#ifndef TEXTUREHOLDER_H
#define TEXTUREHOLDER_H
#include "Textures.h"
#include <map>
#include <string>
#include <memory>
#include <stdexcept>
#include <cassert>
#include <SFML/Graphics.hpp>


class TextureHolder
{
public:
    void load(Textures::ID id, const std::string& filename);


    sf::Texture& get(Textures::ID id);

    const sf::Texture& get(Textures::ID id) const;
private:
    std::map<Textures::ID, std::unique_ptr<sf::Texture>> mTextureMap;
};

#endif // TEXTUREHOLDER_H

 


and the TextureHolder.cpp

#include "TextureHolder.h"


void TextureHolder::load(Textures::ID id,const std::string &filename)
{
    std::unique_ptr<sf::Texture> texture(new sf::Texture());
    if(!texture->loadFromFile(filename))
    {
        throw std::runtime_error("TextureHolder::load - Failed to load " + filename);
    }

    //implement logic error
    auto inserted = mTextureMap.insert(std::make_pair(id,std::move(texture)));
    assert(inserted.second);
}

sf::Texture& TextureHolder::get(Textures::ID id)
{
    auto found = mTextureMap.find(id);
    return *found->second;
}

 

the compiler like I said the default compiler that comes bundled with code::blocks : g++ I think I don't actually know
O and if its important I run ubuntu linux.
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: Nexus on September 26, 2013, 10:25:31 pm
Please describe the problem carefully, there are a lot of confusing parts.

I try to sum up what you've encountered so far:

1. There is a linker error saying "undefined reference to sf::Texture::Texture()", but no compiler error
2. This error occurs only with std::unique_ptr<sf::Texture>, but not with sf::Texture*
3. You do enable C++11 in your project (-std=c++0x or -std=c++11 flag)

If this is right: Can you try to come up with a minimal complete example (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368), as mentioned by eXpl0it3r? I assume the book's original code (https://github.com/SFML/SFML-Game-Development-Book) works?
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: mvl on September 27, 2013, 08:18:34 am
the 3 statements are correct
the code I got from the repository compiles with the C::B projects generated with Cmake but while following along with the book and typing while reading I get this error

I don't know what’s wrong with the example above.
Could you tell me?
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: Nexus on September 27, 2013, 01:27:52 pm
If this is right: Can you try to come up with a minimal complete example (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368), as mentioned by eXpl0it3r?

Please read this link carefully, otherwise we have to ask again...
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: mvl on September 27, 2013, 04:41:30 pm
ok I did some experimenting with unique pointers and turns out I don't get the error if I use it with for example int's
here is a main where the error happens
#include <memory>
#include <SFML/Graphics.hpp>


int main()
{
    std::unique_ptr<sf::Texture> x(new sf::Texture());
    return 0;
}
 
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: Nexus on September 27, 2013, 04:46:10 pm
You compile with C++11 enabled, correct?
What error do you get now?
How do you link SFML?
Title: Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
Post by: mvl on September 27, 2013, 04:55:05 pm
okey turns out the linkers weren’t set which is strange because I was sure I did it!?