0

I was hoping maybe someone could help me solve my issue. I get a lovely

No matching function for call to object of type 'const pCompare'

struct pCompare { bool operator()( const std::string & str1, const std::string & str2 ) const { return str1.compare( str2 ) == 0; } }; std::string *t = new std::string ( "/test.html" ); std::map<std::string*, std::string, pCompare> test; test.insert ( std::pair<std::string*, std::string> ( t, "héhé" ) ); std::cout << test.find(new std::string ("/test.html") )->second; 

Thanks for your help !

3
  • Your map has (const) string * as keys yet your comparator takes const string &... Commented Nov 6, 2012 at 22:02
  • Is it possible to change your implementation to use std::map<std::string, std::string>. Using values instead of pointers can make things a lot easier. Commented Nov 6, 2012 at 22:05
  • 1
    Also note that comparators should test for ordering not equality otherwise you go into UB land. Commented Nov 6, 2012 at 22:13

1 Answer 1

4

First of all, do not do this, as it will screw up your map: You need an ordering function for the map, not an equality test.

Anyway, I guess you problem is that your key type std::string* but your function tests for string& . I haven't tried this, but this should do the trick:

bool operator()( std::string * str1, const std::string * str2 ) const { return *str1 < *str2; } 
Sign up to request clarification or add additional context in comments.

1 Comment

I did forgot the treee part of the map, my bad... In anyway the reference was the issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.