2013-04-02 19 views
7

Próbuję uchwycić koncepcję funkcji wskaźnika w lepszy sposób. Mam więc bardzo prosty i pracuje jako przykład:wskaźniki funkcji generują "nieprawidłowe użycie funkcji niestałego elementu" błąd

#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; 
} 

tak daleko tak dobry, Teraz muszę grupy funkcji w klasie, a następnie wybierz dodawanie lub odejmowanie na podstawie wskaźnika funkcji, które używam. Więc po prostu zrobić małą modyfikację jak:

#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; 
} 

i oczywistym błędzie jest:

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 ja nie określono, które sprzeciwiają się powołać (i nie chcę użyć metody statyczne na razie)

EDIT: kilka komentarzy i odpowiedzi sugeruje, że wersja non-statyczne (jak pisałem) nie jest możliwe (dzięki dla wszystkich) Więc Mod. ifying klasę w następujący sposób również przyzwyczajenie praca:

#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; 
} 

generowany ten błąd:

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)’ 

może wiem jak rozwiązać ten problem, proszę?

dziękuję

+0

Funkcja członkowska domyślnie przyjmuje "ten" jako pierwszy parametr. –

+1

Musisz je ustawić statycznie. W każdym razie wydają się być naturalnie statyczne. –

+0

Wprowadzę zmiany w moim pytaniu. Proszę spojrzeć, a ja ocenię twoje komentarze.dzięki – rahman

Odpowiedz

5

składnia zadeklarować wskaźnik funkcji do metod członkowskich jest:

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

Aby wywołać metody użytkownika Użyj * lub -> * operator.

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

Również spojrzeć na http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspx

Mam nadzieję, że to pomoże.

Kompletny kod:

 #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

Dodawanie & do deklaracji (niezależnie od wywołania) wygeneruje te same błędy. – rahman

+0

pls wypróbować powyższy fragment kodu. Powinno działać. – Arun

+0

działa aun, dzięki – rahman

2

Nie można przekazać funkcji niestatycznego elementu jako argumentu, który jest łatwy. I dla twoich potrzeb, uważam, że lepiej przesłonić operatorów: http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/

Ale jeśli naprawdę potrzebujesz ich jako rzeczywistych funkcji członka - po prostu spraw, aby były statyczne.

+0

nie, to nie jest to, czego potrzebuję. Sądzę, że nie wyjaśniłem wyraźnie problemu. Wprowadzę tylko kilka zmian. Jeśli to też nie pomoże, daj mi znać. dzięki. – rahman

+1

Po zmianach moja odpowiedź jest nadal aktualna. Jak zauważył Alok Save, funkcje składowe wciąż domyślnie traktują to jako pierwszy parametr. Nowe błędy są generowane w rzeczywistości odpowiadając na twoje pytanie: (*) (int, int) deklaracja nie jest (A ::) (int, int) - musisz zmienić deklarację i dereferencji je odpowiednio. Prawdopodobnie powinieneś powiedzieć nam powody, dla których nie możesz mieć ich statycznych lub lepszych, ale mają one nadpisane operatory, ich logika po prostu prosi, aby stały się statyczne :) –

1

zmianę, którą tworzył do kodu nadal jest zły, ponieważ nie sprawiają, że funkcje składowe statyczne. Trzeba uczynić dodatek, odejmowanie itp funkcje statyczne dodając static specyfikator:

#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

Zobacz poniższy kod. Wywołania funkcji działają bez ustawiania ich w sposób statyczny.

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; 
} 
Powiązane problemy