2013-04-02 3 views
7

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

+0

Eine Member-Funktion übernimmt implizit 'this' als ersten Parameter. –

+1

Sie müssen sie statisch machen. Sie scheinen sowieso von Natur aus statisch zu sein. –

+0

Ich werde Änderungen an meiner Frage vornehmen. Bitte schauen Sie und ich werde Ihre Kommentare wertschätzen.danke – rahman

Antwort

5

Die Syntax einer Funktion Zeiger auf Member-Methoden zu erklären ist:

int (A::*plus)(int, int) = &A::add; 
int (A::*minus)(int, int) = &A::subtract; 

aufzurufen verwenden Member-Methoden * oder -> * Operator.

(a_plus.*plus)(7, 5); 

Auch bei http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspx einen Blick

Hoffe das hilft.

komplette Code:

 #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 (A::*functocall)(int, int)) 
    { 
     return (this->*functocall)(first, second); 
    } 
    }; 

    int main() 
    { 
     int a, b; 
     A a_plus, a_minus; 
     int (A::*plus)(int, int) = &A::add; 
     int (A::*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; 
    } 
+1

Das Hinzufügen von & zu Deklarationen (unabhängig vom Aufruf) erzeugt dieselben Fehler. – rahman

+0

Bitte probieren Sie das obige Code-Snippet aus. Es sollte funktionieren. – Arun

+0

es funktioniert arun, danke – rahman

2

Sie können nicht nicht-statische Member-Funktion als Argument, das leicht passieren. Und für Ihre Bedürfnisse glaube ich, dass es besser ist, Operatoren zu überschreiben: http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/

Aber wenn Sie sie wirklich als tatsächliche Elementfunktionen benötigen - machen Sie sie einfach statisch.

+0

nein, das ist nicht was ich brauche. Ich denke, ich habe das Problem nicht klar erklärt. Ich werde nur ein paar Änderungen vornehmen. Wenn das auch keine Lösung hat, lass es mich wissen. Vielen Dank. – rahman

+1

Nach Änderungen steht meine Antwort immer noch. Wie Alok Save darauf hingewiesen hat, haben Elementfunktionen dies immer noch als ersten Parameter implizit. Neue Fehler werden erzeugt, die tatsächlich Ihre Frage beantworten: (*) (int, int) Deklaration ist nicht (A ::) (int, int) - Sie müssen die Deklaration ändern und sie entsprechend dereferenzieren. Sie sollten uns wahrscheinlich Gründe nennen, warum Sie sie nicht statisch oder besser als überschriebene Operatoren haben können. Ihre Logik fordert nur, sie statisch zu machen. –

1

Das bearbeiten Sie Ihren Code gemacht ist immer noch falsch, weil es nicht die Mitgliederfunktionen statisch macht. Sie müssen das Add machen, subtrahieren usw. Funktionen statisch durch Zugabe des static Spezifizierer:

#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; 
    } 
static int add(int first, int second) 
{ 
    return first + second; 
} 

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

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

den Code unten sehen. Die Funktionsaufrufe funktionieren, ohne sie statisch zu machen.

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(A::*functocall)(int, int)) 
    { 
     return (this->*functocall)(first, second); 
    } 
}; 
//typedef int(A::*PFN)(int, int) ; 
int main() 
{ 
    int a, b; 
    A a_plus, a_minus; 
    a = a_plus.operation(7, 5, &A::add); 
    b = a_minus.operation(20, a, &A::subtract); 
    cout << "a = " << a << " and b = " << b << endl; 
    return 0; 
}