Funktion Zeiger generieren 'ungültige Verwendung der nicht-statischen member-Funktion' error

Ich versuche zu begreifen, Zeiger Funktions-Konzept in einem besseren Weg. Also ich habe ein sehr einfach und funktioniert beispielsweise so:

#include <iostream>

using namespace std;

int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}

int main()
{
    int  a, b;
    int  (*plus)(int, int);
    int  (*minus)(int, int);
    plus = &add;
    minus = &subtract;
    a = operation(7, 5, add);
    b = operation(20, a, minus);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}

So weit So gut,
Jetzt brauche ich für die Gruppe der Funktionen in eine Klasse, und wählen Sie hinzufügen oder subtrahieren auf der Grundlage der Funktion der Zeiger, die ich benutze. Also ich mache nur eine kleine änderung, wie:

#include <iostream>

using namespace std;

class A
{
public:
int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
};

int main()
{
    int  a, b;
    A a_plus, a_minus;
    int (*plus)(int, int) = A::add;
    int (*minus)(int, int) = A::subtract;
    a = a_plus.operation(7, 5, plus);
    b = a_minus.operation(20, a, minus);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}

- und die offensichtlichen Fehler:

ptrFunc.cpp: In function int main()’:
ptrFunc.cpp:87:29: error: invalid use of non-static member function int A::add(int, int)’
ptrFunc.cpp:88:30: error: invalid use of non-static member function int A::subtract(int, int)’

coz ich noch nicht angegeben, welches Objekt zum aufrufen(und ich don ' T wollen, verwenden Sie die statischen Methoden für jetzt)

EDIT:
mehrere Kommentare und Antworten vorgeschlagen, dass die nicht-statische version(wie ich geschrieben habe) ist nicht möglich.(Dank an alle)
So,
Ändern Sie die Klasse in der folgenden Art und Weise auch nicht funktionieren:

#include <iostream>

using namespace std;

class A
{
    int res;
public:
    A(int choice)
    {
        int (*plus)(int, int) = A::add;
        int (*minus)(int, int) = A::subtract;
        if(choice == 1)
            res = operation(7, 5, plus);
        if(choice == 2)
            res = operation(20, 2, minus);
        cout << "result of operation = " << res;
    }
int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
};

int main()
{
    int  a, b;
    A a_plus(1);
    A a_minus(2);
    return 0;
}

diesen Fehler erzeugt:

ptrFunc.cpp: In constructor A::A(int)’:
ptrFunc.cpp:11:30: error: cannot convert A::add from type int (A::)(int, int)’ to type int (*)(int, int)’
ptrFunc.cpp:12:31: error: cannot convert A::subtract from type int (A::)(int, int)’ to type int (*)(int, int)’

darf ich wissen, wie dieses Problem zu lösen bitte?

Dank

  • Eine member-Funktion implizit nimmt this als ersten parameter.
  • Sie müssen, um Sie statisch. Sie scheinen naturgemäß statisch sowieso.
  • Werde ich änderungen an meiner Frage. Bitte haben Sie einen Blick und ich Wert auf Ihre Kommentare. Dank
InformationsquelleAutor rahman | 2013-04-02
Schreibe einen Kommentar