Archiv verlassen und diese Seite im Standarddesign anzeigen : Problem mit C++ lerrne+
Gast@C++ am lernen
2007-08-14, 16:05:31
Hallo alle zusammen
bin momentan am C++ am lernen.
Bin gerade beim Theman: Array mit Zeigern auf Elementfunktionen.
Hab hier einen Code und finde den Fehler nicht.
#include <iostream.h>
class Dog
{
public:
void Speak() const {cout << "Wuff!\n";}
void Move() const {cout << "Laufen...\n";}
void Eat() const {cout << "Fressen...\n";}
void Growl() const {cout << "Grrrrr\n";}
void Whimper() const {cout << "Heulen...\n";}
void RollOver() const {cout << "Herumwaelzen...\n";}
void PlayDead() const {cout << "Ist dies das Ende von Caesar?\n";}
};
typedef void (Dog::*PDF)()const;
int main()
{
const int MaxFuncs = 7;
PDF DogFunctions[MaxFuncs]=
{
Dog::Speak,
Dog::Move,
Dog::Eat,
Dog::Growl,
Dog::Whimper,
Dog::RollOver,
Dog::PlayDead };
while(!fQuit)
{
cout << "(0)Beenden (1)Sprechen(2)Bewegen (3)Fressen (4)Knurren";
cout << " (5)Winseln (6)Herumwaelzen (7)Totstellen: ";
cin >> Method;
if (Method==0)
{
fQuit = true;
}
else
{
pDog=new Dog;
(pDog->*DogFunctions[Method-1])();
delete pDog;
}
}
return 0;
}
Wäre nett, wenn mir jemand helfen würde.
Vielen dank.
Hallo alle zusammen
bin momentan am C++ am lernen.
Bin gerade beim Theman: Array mit Zeigern auf Elementfunktionen.
Hab hier einen Code und finde den Fehler nicht.
Wäre nett, wenn mir jemand helfen würde.
Vielen dank.
Leider sind mit da paar Fehler aufgetaucht.
Hab mich beim titel vertipt und diese Smile soll da nicht hin. (Fehler von der webseite.da soll " : : P" stehen (also ohne die leerzeichen)).
Poste das mal nochmal mit einem code-Tag statt quote. Das ist unlesbar.
Ups sorry.Kenne mich mit den Forum eigenschaften noch nicht so aus.
<code>
#include <iostream.h>
class Dog
{
public:
void Speak() const {cout << "Wuff!\n";}
void Move() const {cout << "Laufen...\n";}
void Eat() const {cout << "Fressen...\n";}
void Growl() const {cout << "Grrrrr\n";}
void Whimper() const {cout << "Heulen...\n";}
void RollOver() const {cout << "Herumwaelzen...\n";}
void PlayDead() const {cout << "Ist dies das Ende von Caesar?\n";}
};
typedef void (Dog::*PDF)()const;
int main()
{
const int MaxFuncs = 7;
PDF DogFunctions[MaxFuncs]=
{
Dog::Speak,
Dog::Move,
Dog::Eat,
Dog::Growl,
Dog::Whimper,
Dog::RollOver,
Dog::PlayDead };
while(!fQuit)
{
cout << "(0)Beenden (1)Sprechen(2)Bewegen (3)Fressen (4)Knurren";
cout << " (5)Winseln (6)Herumwaelzen (7)Totstellen: ";
cin >> Method;
if (Method==0)
{
fQuit = true;
}
else
{
pDog=new Dog;
(pDog->*DogFunctions[Method-1])();
delete pDog;
}
}
return 0;
}
</code>
Hoffe, das es si richtig ist
Jetzt aber.
#include <iostream.h>
class Dog
{
public:
void Speak() const {cout << "Wuff!\n";}
void Move() const {cout << "Laufen...\n";}
void Eat() const {cout << "Fressen...\n";}
void Growl() const {cout << "Grrrrr\n";}
void Whimper() const {cout << "Heulen...\n";}
void RollOver() const {cout << "Herumwaelzen...\n";}
void PlayDead() const {cout << "Ist dies das Ende von Caesar?\n";}
};
typedef void (Dog::*PDF)()const;
int main()
{
const int MaxFuncs = 7;
PDF DogFunctions[MaxFuncs]=
{
Dog::Speak,
Dog::Move,
Dog::Eat,
Dog::Growl,
Dog::Whimper,
Dog::RollOver,
Dog::PlayDead };
while(!fQuit)
{
cout << "(0)Beenden (1)Sprechen(2)Bewegen (3)Fressen (4)Knurren";
cout << " (5)Winseln (6)Herumwaelzen (7)Totstellen: ";
cin >> Method;
if (Method==0)
{
fQuit = true;
}
else
{
pDog=new Dog;
(pDog->*DogFunctions[Method-1])();
delete pDog;
}
}
return 0;
}
rotalever
2007-08-14, 16:29:41
Also bei mir kompilierts noch nicht mal. Erstmal den namespace hinzufügen.
2. ist das Programm auch sonst irgendwie nicht vollständig, zum Beispiel werden Variablen nicht deklariert.
3. Für dogfunctions[..] = blabla kommt als Fehler
Fehler: ungültige Verwendung der nicht-statischen Elementfunktion »void Dog::Speak() const«
[...]
Erstmal: iostream.h gibt es nicht. es ist <iostream>
Desweiteren ist da extrem viel Murks drin. Willst du uns verarschen? Fang doch erstmal klein an anstatt mit Funktionspointern zu hantieren.
hm hm hm hab mit c++ schon ziemlich lange nix mehr gemacht aber versuch mal, entweder vor jedes void in der Klasse Dog noch static zu schreiben.
Besserist es jedoch, mit Dog* d = new Dog() eine neue Instanz von Dog zu erstellen und dann in der Definition von DogFunctions anstatt Dog.Funktionsname d->Funktionsname zu schreiben.
Dieser Quellcode steht auch im Buch " C++ in 21 Tagen " von Jesse Liberty.
Als Compiler nutze ich Dev-C++.
Kann auch sein, das wie manches in diesen Buchfehlerhaft ist.
Jedoch konnte ich bis jetzt immer den Fehler finden.
Sehe gerade, das der Code nícht ganz vollständig ist.
#include <iostream.h>
class Dog
{
public:
void Speak() const {cout << "Wuff!\n";}
void Move() const {cout << "Laufen...\n";}
void Eat() const {cout << "Fressen...\n";}
void Growl() const {cout << "Grrrrr\n";}
void Whimper() const {cout << "Heulen...\n";}
void RollOver() const {cout << "Herumwaelzen...\n";}
void PlayDead() const {cout << "Ist dies das Ende von Caesar?\n";}
};
typedef void (Dog::*PDF)()const;
int main()
{
const int MaxFuncs = 7;
PDF DogFunctions[MaxFuncs]=
{
Dog::Speak,
Dog::Move,
Dog::Eat,
Dog::Growl,
Dog::Whimper,
Dog::RollOver,
Dog::PlayDead };
Dog* pDog =0;
int Method;
bool fQuit = false;
while(!fQuit)
{
cout << "(0)Beenden (1)Sprechen(2)Bewegen (3)Fressen (4)Knurren";
cout << " (5)Winseln (6)Herumwaelzen (7)Totstellen: ";
cin >> Method;
if (Method==0)
{
fQuit = true;
}
else
{
pDog=new Dog;
(pDog->*DogFunctions[Method-1])();
delete pDog;
}
}
return 0;
}
@ rotalever
Genau diesen Fehler bekomme ich auch.
invalid use of non-static member function `void Dog::Speak() const'
und zwar von der Zeile 22 bis 28.
um sich einen Funktor zu holen benutzt man & ... es soll auch ohne gehen, aber das ist nicht Standartkonform
um sich einen Funktor zu holen benutzt man & ... es soll auch ohne gehen, aber das ist nicht Standartkonform
Vielen dank.
Das war der Fehler.
Man dieser Compiler will auch immer extra wünsche. :-)
vBulletin®, Copyright ©2000-2025, Jelsoft Enterprises Ltd.