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

Author Topic: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>  (Read 4441 times)

0 Members and 1 Guest are viewing this topic.

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
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!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
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 ...

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
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?!...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
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, as mentioned by eXpl0it3r? I assume the book's original code works?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
If this is right: Can you try to come up with a minimal complete example, as mentioned by eXpl0it3r?

Please read this link carefully, otherwise we have to ask again...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
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;
}
 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
« Reply #10 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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: error while folowing book sfml game devellopment std::unique_ptr<sf::Texture>
« Reply #11 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!?

 

anything