Question

I have a map variable called x which is defined as :

    std::map <std::string, std::string, std::less<std::string>,
    MyAlloc<std::pair <std::string, std::string> > >
    x (std::less<std::string> (), MyAlloc <std::pair <std::string, std::string> > ());

where MyAlloc is my custom STL Allocator defined by me. When I try to insert a pair into the map, like :

    x.insert(std::pair<std::string,std::string>("key","value"));

I'm getting the following error :

    error: request for member ‘insert’ in ‘x’, which is of non-class type
   ‘std::map<std::basic_string<char>, std::basic_string<char>, 
    std::less<std::basic_string<char> >, MyAlloc<std::pair<std::basic_string<char>,
    std::basic_string<char> > > >(std::less<std::basic_string<char> > (*)(), 
    MyAlloc<std::pair<std::basic_string<char>, std::basic_string<char> > > (*)())’

I don't get this error if I'm using the default constructor of the Allocator, like :

    std::map <std::string, std::string, std::less<std::string>,MyAlloc <std::pair <std::string, std::string> > > x;

I'm not sure whether the intialisation with the default constructor is correct.. ?

Thanks in advance .. :)

Was it helpful?

Solution

The keyword to look for here is "most vexing parse". The definition of x is parsed as a function declaration. Wikipedia has an example which mentions the exact same error message.

OTHER TIPS

You've defined a function x, not an object.

The default ctor would use the same comparison and allocator, so use that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top