Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback function: Incompatible argument

Tags:

c++

callback

I am trying to use callback function in my problem but I got into some troubles. In the sort() function, the parameter &compareType has an error:

Argument of type "bool (Person::*)(const Person& p1, const Person& p2)" is incompatible with parameter of type "compare"`

person.h

class Person
{
public:
    bool compareType(const Person& p1, const Person& p2) { return ... };
    void sort()
    {
        ...    
        list->addInOrder(person, &compareType);
        ...
    }
    ...
}

dlinkedlist.h

typedef bool (*compare)(const Person& p1, const Person&p2);
class dlinkedlist
{
public:
    void addInOrder(const Person& person, compare comparefunc)
    {
        Person person2;
        ...
        comparefunc(person, person2);
        ...
    }
}
like image 284
Stoatman Avatar asked Apr 16 '26 12:04

Stoatman


1 Answers

bool compareType(const Person& p1, const Person& p2)

is actually of type

bool (Person::*) (const Person&, const Person&)

You have to make your method static to have correct type.

like image 87
Jarod42 Avatar answered Apr 19 '26 02:04

Jarod42