M
mdaniela
Neues Mitglied
Thread Starter
- Dabei seit
- 03.03.2006
- Beiträge
- 16
- Reaktionspunkte
- 0
Hallo Community!
Ich bin noch C++ Neuling und erlerne mir die Sprache Schritt für Schritt auf meinem Mac. Ich bin nun auf ein Beispiel gestoßen, in dem ich einen Pointer erzeuge, der auf eine Klassenmethode zeigt.
Das Beispiel stammt aus dem Buch "C++ in 21 Tagen" - eher auf Windows ausgerichtet - und lässt sich auf Mac nicht kompelieren.
Meine Frage: Muss ich einfach anders an das Beispiel rangehen oder gibt's unter Mac keine Pointer auf Klassenmethoden? Zweiteres kann ich mir kaum vorstellen ...
[Beispiel siehe unten]
Z 66 + 67 bringen in xCode folgende Fehlermeldung:
error: invalid use of non-static member function 'virtual void Animal::speak() const'
error: cannot convert 'void ( Animal:: ) ()const' to 'void (Animal::*)()const' in assignment
#include <iostream.h>
4:
5: class Mammal
6: {
7: public:
8: Mammal():itsAge(1) { }
9: virtual ~Mammal() { }
10: virtual void Speak() const = 0;
11: virtual void Move() const = 0;
12: protected:
13: int itsAge;
14: };
15:
16: class Dog : public Mammal
17: {
18: public:
19: void Speak()const { cout << "Wuff!\n"; }
20: void Move() const { cout << "Bei Fuß gehen ...\n"; }
21: };
22:
23:
24: class Cat : public Mammal
25: {
26: public:
27: void Speak()const { cout << "Miau!\n"; }
28: void Move() const { cout << "Schleichen...\n"; }
29: };
30:
31:
32: class Horse : public Mammal
33: {
34: public:
35: void Speak()const { cout << "Wieher!\n"; }
36: void Move() const { cout << "Gallopieren...\n"; }
37: };
38:
39:
40: int main()
41: {
42: void (Mammal::*pFunc)() const =0;
43: Mammal* ptr =0;
44: int Animal;
45: int Method;
46: bool fQuit = false;
47:
48: while (fQuit == false)
49: {
50: cout << "(0)Beenden (1)Hund (2)Katze (3)Pferd: ";
51: cin >> Animal;
52: switch (Animal)
53: {
54: case 1: ptr = new Dog; break;
55: case 2: ptr = new Cat; break;
56: case 3: ptr = new Horse; break;
57: default: fQuit = true; break;
58: }
59: if (fQuit)
60: break;
61:
62: cout << "(1)Sprechen (2)Bewegen: ";
63: cin >> Method;
64: switch (Method)
65: {
66: case 1: pFunc = Mammal::Speak; break;
67: default: pFunc = Mammal::Move; break;
68: }
69:
70: (ptr->*pFunc)();
71: delete ptr;
72: }
73: return 0;
74: }
Bin für jede Hilfe dankbar!
Ich bin noch C++ Neuling und erlerne mir die Sprache Schritt für Schritt auf meinem Mac. Ich bin nun auf ein Beispiel gestoßen, in dem ich einen Pointer erzeuge, der auf eine Klassenmethode zeigt.
Das Beispiel stammt aus dem Buch "C++ in 21 Tagen" - eher auf Windows ausgerichtet - und lässt sich auf Mac nicht kompelieren.
Meine Frage: Muss ich einfach anders an das Beispiel rangehen oder gibt's unter Mac keine Pointer auf Klassenmethoden? Zweiteres kann ich mir kaum vorstellen ...
[Beispiel siehe unten]
Z 66 + 67 bringen in xCode folgende Fehlermeldung:
error: invalid use of non-static member function 'virtual void Animal::speak() const'
error: cannot convert 'void ( Animal:: ) ()const' to 'void (Animal::*)()const' in assignment
#include <iostream.h>
4:
5: class Mammal
6: {
7: public:
8: Mammal():itsAge(1) { }
9: virtual ~Mammal() { }
10: virtual void Speak() const = 0;
11: virtual void Move() const = 0;
12: protected:
13: int itsAge;
14: };
15:
16: class Dog : public Mammal
17: {
18: public:
19: void Speak()const { cout << "Wuff!\n"; }
20: void Move() const { cout << "Bei Fuß gehen ...\n"; }
21: };
22:
23:
24: class Cat : public Mammal
25: {
26: public:
27: void Speak()const { cout << "Miau!\n"; }
28: void Move() const { cout << "Schleichen...\n"; }
29: };
30:
31:
32: class Horse : public Mammal
33: {
34: public:
35: void Speak()const { cout << "Wieher!\n"; }
36: void Move() const { cout << "Gallopieren...\n"; }
37: };
38:
39:
40: int main()
41: {
42: void (Mammal::*pFunc)() const =0;
43: Mammal* ptr =0;
44: int Animal;
45: int Method;
46: bool fQuit = false;
47:
48: while (fQuit == false)
49: {
50: cout << "(0)Beenden (1)Hund (2)Katze (3)Pferd: ";
51: cin >> Animal;
52: switch (Animal)
53: {
54: case 1: ptr = new Dog; break;
55: case 2: ptr = new Cat; break;
56: case 3: ptr = new Horse; break;
57: default: fQuit = true; break;
58: }
59: if (fQuit)
60: break;
61:
62: cout << "(1)Sprechen (2)Bewegen: ";
63: cin >> Method;
64: switch (Method)
65: {
66: case 1: pFunc = Mammal::Speak; break;
67: default: pFunc = Mammal::Move; break;
68: }
69:
70: (ptr->*pFunc)();
71: delete ptr;
72: }
73: return 0;
74: }
Bin für jede Hilfe dankbar!