What I am trying to do is to have basically a key value store in a c++ class so that I can lookup values from it by the key. I've been trying to use an unordered_map to do it with code similar to this. (I've simplified it a little but you get the idea).
#include <string>
#include <unordered_map>
#include <iostream>
class manager
{
public:
manager() {}
~manager(){}
void add(const std::string& name, unsigned int val) {
map.insert(std::make_pair<std::string, unsigned int>(name, val));
}
unsigned int GetValue(const std::string& key) {
return map[key];
}
std::unordered_map<std::string, unsigned int> map;
};
int main(void)
{
manager* mgr = new manager();
mgr->add("Bob",22);
std::cout << "Bob is" << mgr->GetValue("Bob") << std::endl;
return 0;
}
I just want to store a name and value in a way that I can easily lookup the value by name, and easily clean it up when done to avoid memory leaks.
When I compile this on Linux (g++ -o test test.cpp) I get the following:
test.cpp: In member function ‘void manager::add(const string&, unsigned int)’: test.cpp:12:65: error: no matching function for call to ‘make_pair(const string&, unsigned int&)’
map.insert(std::make_pair(name, val)); ^ In file included from /usr/include/c++/6/bits/stl_algobase.h:64:0, from /usr/include/c++/6/bits/char_traits.h:39, from /usr/include/c++/6/string:40, from test.cpp:1: /usr/include/c++/6/bits/stl_pair.h:497:5: note: candidate: template constexpr std::pair::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) make_pair(_T1&& __x, _T2&& __y) ^~~~~~~~~ /usr/include/c++/6/bits/stl_pair.h:497:5: note: template argument deduction/substitution failed: test.cpp:12:56: note: cannot convert ‘name’ (type ‘const string {aka const std::__cxx11::basic_string}’) to type ‘std::__cxx11::basic_string&&’
map.insert(std::make_pair(name, val));