views:

498

answers:

2

Why I cant insert like here?

#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 give me many errors and I dont 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

+2  A: 

You need to

#include <string>
nos
wow ... so fast, and so stupid mistake.. i didnt realize this mistake.. but why 17 errors for this simple thing ? :S
I'm more surprised that it let the several other references to std::string compile before complaining about that one...
Tyler McHenry
Template errors cascade. If it cannot do operator< on a string, then it cannot do X on Y, then Y on Z, etc...
GMan
Hm, gcc borks on line 7 with: "string is not a member of std". A lot clearer.
pmr
+1  A: 

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'.

Martin York