C++ Map can't insert with pair
-
11-07-2019 - |
Question
Why can't I insert as shown below?
#include <map>
struct something {
} some_object;
typedef std::map<std::string, something*> list;
typedef std::pair<std::string, something*> pair;
int main()
{
list l;
pair p("abc", &some_object); // working fine!!!
l.insert(p); // 17 errors
return 0;
}
Visual studio gives me many errors and I don't understand anything of them. The first one is:
error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
I can post more but I don't want to spam here. Thanks a lot for your help.
Solution
You need to
#include <string>
OTHER TIPS
I would change this line:
typedef std::pair<std::string, something*> pair;
You are relaying on an implementation detail. Are you sure this wil always be true for all future version of the library? Tightly coupling your code like that is a bad idea.
Try this:
typedef list::value_type pair;
PS. 'list' would not be my first choice for the name of a type I put in the global namespace. Either put it in your own namespace or call it 'MyList'.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow