1

Codeblocks raises an error on this line :

set<string,cmpi> m;

Where the cmpi function is :

int cmpi(string one , string two )
{
    one = toLowerCase(one); 
    two = toLowerCase(two);

    if(two == one) 
        return 0;
    else 
    if (one < two ) 
        return -1;
    else 
        return 1;
}

It says (the ERROR) :

type/value mismatch at argument 2 in template parameter list for 'template<class _Key, class _Compare, class _Alloc> class std::set'

Is there something with the return value of my cmpi function or is it something else ?

wkl
  • 77,184
  • 16
  • 165
  • 176
Black Zach
  • 11
  • 1
  • 2
    You have to supply a functor name, not a function's name. A functor is demonstrated in this question, which is the same problem as yours: http://stackoverflow.com/questions/2620862/using-custom-stdset-comparator. As an aside, your `cmpi` does what `std::string`'s overloaded comparison operators already do, so you don't need to supply a comparator. – wkl Oct 01 '11 at 02:32
  • @birryree: No, his compare function makes the strings lowercase before comparing them. That isn't the same at all. – Zan Lynx Oct 01 '11 at 02:45
  • @Zan - whoops...someone needs sleep. Thanks. – wkl Oct 01 '11 at 02:46
  • 1
    Although you can use a function pointer, the type that you would want would be something like `std::set`. However, you need to proved a comparsion that does the job of "less than", not a three way +1/0/-1 style comparison so your compare should be returning `true` if `one < two` and `false` otherwise. – CB Bailey Oct 01 '11 at 03:26

2 Answers2

2

type/value mismatch

Indeed. std::set expects a type, not a function pointer (value):

int cmpi(string one, string two);

typedef int cmpi_t(string one, string two); // the type of cmpi

std::set<string, cmpi_t*> m (&cmpi);
curiousguy
  • 8,038
  • 2
  • 40
  • 58
  • You either need `typedef int (*cmpi_t)(string, string);` or `std::set`. The function template argument can't be a function type, it must be a pointer to function type. – CB Bailey Oct 01 '11 at 03:33
  • @CharlesBailey Obviously, a star was missing somewhere. (I am ashamed.) – curiousguy Oct 01 '11 at 03:47
1

The second parameter has to be a type. You can create a type for your function like this:

struct CmpI {
  bool operator()(const string &a,const string &b) { return cmpi(a,b)<0; }
};
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132