c++ - std::unique_ptr instantiate error trying to place inside std::map? -
so i'm using sfml , boost libraries trying write resourcemanager class. i'm using std::map contain resources. have heard std::unique_ptr because of memory cleanup (or along lines).
this resourcemanager class looks like:
#pragma once #include <boost/any.hpp> #include <map> #include <memory> #include <sfml/graphics.hpp> #include "resource.h" class resourcemanager { public: resourcemanager(); void clear(); void dump(); boost::any getresource(std::string s); sf::texture loadtexture(std::string s, sf::intrect d); void unloadtexture(std::string s); private: std::map<std::string, std::unique_ptr<boost::any>> resource; };
here method i'm trying use load objects map
sf::texture resourcemanager::loadtexture(std::string s, sf::intrect d) { std::unique_ptr<sf::texture> t; if (!t->loadfromfile(s, d)) std::cout << "error loading resource: " << s << std::endl; resource[s] = t; }
std::unique_ptr
move-only time, meaning cannot copy it.
to fix it, use std::move
:
resource[s] = std::move (t);
if copied did, have 2 unique_ptr
's pointing same object (which non-sense since unique), must move it, calling move-assignment-operator.
Comments
Post a Comment