invalid initialization of reference

1
2
3
4
5
6
7
8
9
10
11
12
template <typename Resource, typename Identifier>
class ResourceHolder
{
private:
	std::map<Identifier, std::unique_ptr<Resource>> mMap;
public:
	Resource & get( Identifier);
	const Resource & get(Identifier) const;
	void load( Identifier, const std::string &);
	template <class Parameter>
	void load( Identifier, const std::string &, Parameter &);
};

...
1
2
3
4
5
6
7
8
9
Resource &
ResourceHolder<Resource,Identifier>::get(Identifier id)
{
    auto resIter = mMap.find(id);
    if (resIter == mMap.end()) {
    	throw std::runtime_error("ResourceHolder::get - ID not found.");
    }
    return *resIter;  // error message refers to this line
}

I get this error message
error: invalid initialization of reference of type ‘Texture&’ ...

Any idea what could be wrong?
Last edited on
resIter is an iterator to an object of type std::unique_ptr<Resource>.

So *resIter is an object of type std::unique_ptr<Resource>

So you're trying to return an object of type std::unique_ptr<Resource> from a function that says it returns an object of type Resource &
Type of resIter is probably std::map<Identifier, std::unique_ptr<Resource>>::iterator

mMap::value_type is an alias for pair<const Identifier, std::unique_ptr<Resource>>

Dereferencing iterator returns value_type, does it not?

Does Resource look like pair<const Identifier, std::unique_ptr<Resource>>?
Thank you for your help!
*resIter didn't return Resource&, but rather it returned a std::pair. so all I needed was changing return *resIter; to return *resIter->second;.
Topic archived. No new replies allowed.