Witam, mam szablon klasy ktory przechowuje rozne obiekty (textury dzwieki itp). Sprawa dotyczy metody get ktora ma zwrócić referencje na konkretny podany w argumencie element.
Jak to wyglada.
template<typename T>
class ResourceManager
{
public:
ResourceManager()=default;
ResourceManager(const ResourceManager&)=delete;
ResourceManager operator =(const ResourceManager&)=delete;
virtual void loadFromFile(string ,string &);
T& operator[](string &);
const T& get(const string&);
protected:
map<string ,T*> m_resources;
};
template<typename T>
const T& ResourceManager<T>::get(const string &name)
{
assert(m_resources.find(name)==m_resources.end());
/// Kompilator wskazuje na tego returna
return m_resources[name];
}
I wywolanie
ResourceManager<sf::Texture> TextureManager;
/// Zaladowywuje
for (string texture : { "Mario", "Sky", "AnimTiles", "Enemies", "Bowser", "Items", "Logo" })
{
string which=texture + ".png";
getTexturManager().loadFromFile(texture,which);
}
Sprite m_background;
m_background.setTexture(TextureManager.get("Sky"));
I komunikat kompilatora
invalid initialization of reference of type 'const sf::Texture&' from expression of type 'std::map<std::basic_string<char>, sf::Texture*, std::less<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, sf::Texture*> > >::mapped_type {aka sf::Texture*}'
Z góry dzięki i prosze o pomoc bo blokuje mnie to w dalszym pisaniu.