Ich versuche, Zeigerfunktion Konzept auf eine bessere Weise zu begreifen. So habe ich eine sehr einfache und Arbeits Beispiel als:Funktion Zeiger erzeugen 'ungültige Verwendung von nicht-statische Mitglied Funktion' Fehler
#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 habe ich zu einer Gruppe müssen die Funktionen in einer Klasse, und wählen Sie addieren oder subtrahieren basierend auf dem Funktionszeiger, die ich benutze. Also mache ich 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 offensichtliche Fehler ist:
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 I nicht angegeben haben, die zum Aufrufen Objekt (und ich will nicht statische Methoden verwenden jetzt)
EDIT: mehr Kommentare und Antworten vorgeschlagen, dass die nicht-statische Version (wie ich geschrieben habe) nicht möglich ist (dank an alle) so Mod. ifying die Klasse auf die folgende Weise pflegt auch Arbeit:
#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 generiert:
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)’
kann ich wissen, wie dieses Problem zu lösen, bitte?
dank
Eine Member-Funktion übernimmt implizit 'this' als ersten Parameter. –
Sie müssen sie statisch machen. Sie scheinen sowieso von Natur aus statisch zu sein. –
Ich werde Änderungen an meiner Frage vornehmen. Bitte schauen Sie und ich werde Ihre Kommentare wertschätzen.danke – rahman