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!
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.
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;
}