mercredi 30 mars 2016

cannot convert ‘void (myClass::*)() to void (*)()

So, I know this question exists in so many places. But none of the examples helped me solve my issue.

I'm trying to create a method pointer (within a class), so it will address one of several methods of the class according to specific conditions.

I tried, unsuccessfully to use a static function (guess I misunderstood the instructions how to do so...).

here is the header file:

class myClass
{
public:
    myClass(int value);

    void methodA(const string &msg);
    void methodB(const string &msg);

    void (*send_msg)(const string &msg);
};

and the cpp:

myClass::myClass(int value){
    if(value > 0){
        send_msg = &methodA
    }
    else{
        send_msg = &methodB
    }
}

and the errors, as some of you already know:

error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&myClass::methodA’ [-fpermissive]

error: cannot convert ‘void (myClass::)(const string&) {aka void (myClass::)(const std::basic_string&)}’ to ‘void ()(const string&) {aka void ()(const std::basic_string&)}’ in assignment

any help will be much appreciated.

Aucun commentaire:

Enregistrer un commentaire