开发者

std::set constructor second argument error

开发者 https://www.devze.com 2023-04-10 01:36 出处:网络
Codeblocks raises an error on this line : set<string,cmpi> m; Where the cmpi function is : int cmpi(string one , string two )

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/v开发者_开发百科alue 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 ?


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);


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; }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消