Edit:
The data is as follows
typedef std::shared_ptr<T> Resource;
typedef std::map<std::string, Resource> ResourceMap;
This is the function
const T& Get(const std::string& key)
{
ResourceMap::iterator itr = mResources.find(key);
return (itr != mResources.end()) ? itr->second : mDefault;
}
Error:
Error 1 error C2446: ':' : no conversion from 'sf::Texture' to 'std::tr1::shared_ptr<_Ty>' d:\sanity\trunk\client\src\assetmanager.h 28
Also I’m creating an object like that :
AssetManager<sf::Texture> imgManager;
Okay, sorry for missing information, I was in a rush.
The only problem left is :
return (itr != mResources.end()) ? itr->second.get() : mDefault;
get() returns raw pointer and I need to return reference to what the shared_ptr is pointing.
How should I do that?
I guess your problem is that
itr->secondis of one type (Resource, if yourstd::maptypedef is correct), whilemDefaultmust be something other.The ternary operator cannot handle the two different types, so you must correct your code to be sure both items left and right of the
:part of the?:operator are of the same type (or compatible ones).So confirm this, I need:
ResourcetypeTtypemDefaultmember variableEdit
Let’s assume you have something like:
instanciated like:
Now, I need the type of
mDefaultto continue.My guess: You MUST make sure your code is more like:
As you want to return the
Resource, not the shared_ptr of theResource.Note that I added
constkeywords/prefix to be consistent with theconstreturnEdit 2
As you have:
So I guess you should write:
itr->secondis a smart pointer, so if you want to get the pointer object, you simply need to dereference the smart pointer:*(itr->second).As for returning a reference to mDefault, this is indicated by the function’s return type
const T &, so you don’t need anything more.