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
Du musst angemeldet sein, um einen Kommentar abzugeben.
Die syntax zum deklarieren einer Funktion Zeiger auf member-Methoden ist:
Aufrufen member-Methoden verwenden .* oder ->* Betreiber:
Haben auch einen Blick auf http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs. 80).aspx
Hoffe, das hilft.
Komplette code:
Können Sie T-pass-nicht-statische member-Funktion als argument, dass einfach. Und für Ihre Bedürfnisse, ich glaube, es ist besser, zu überschreiben Betreiber: http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/
Aber wenn Sie wirklich brauchen, Sie als echte member-Funktionen - nur dass Sie statisch.
Bearbeiten Sie Ihre code ist immer noch falsch, weil es nicht um die statische member-Funktionen. Sie brauchen, um machen, addieren, subtrahieren etc. Funktionen statische, indem die
static
Planer:Siehe unten im code. Die Funktionsaufrufe sind zu arbeiten, ohne dass Sie statisch.