I’m getting the following error when trying to build:
error: invalid initialization of non-const reference of type "std::vector<......"
Code:
class Config {
public:
std::vector<string>& GetValuesForList(std::string listName);
private:
std::map<std::string, std::vector<string> > lists;
};
inline std::vector<string>&
Config::GetValuesForList(std::string listName) {
return lists.find(listName);
}
I’ve read up on it and seems to be because of C++ temporaries, but am unsure how to resolve it. Any help would be greatly appreciated.
Thanks.
map::find returns iterator. So you should use it’s second value: