|
From: Roger F. <ra...@ha...> - 2004-09-06 00:08:00
|
Aloha from Honolulu,
The following was taken from Sams Teach Yourself C++ and refuses to
compile. The compiler reports the error at the line following
"Dog::PlayDead" which is the closing brace and semi-colon for the "PDF
DogFunctions...". I have marked it with an exclamation point ! here.
While I do need to know what the code's problem is, more vexing is that
in the compiler report window under messages is listed "assuming" and on
the next line "(a". Nowhere have I been able to find a list of the
various messages. I looked under the Help window of the program
(Version 4.9.8.0) and also on the Bloodshed site, but no luck there
either. I would suggest that a comprehensive list of error messages
would benefit most everyone using these programs.
A further note: earlier in the lesson was a line: enum BOOL {FALSE,
TRUE}; that generated the folowing error messages: "conflicting" and
then: (copied from the compiler report window)
227 C:\Dev-Cpp\include\windef.h previous declaration as `typedef
WINBOOL BOOL'
My solution was to comment it out, but why did the message appear
when windef.h was never included in the code?
Thanks in advance,
Mahalo,
Roger
//Listing 20.10 Array of pointers to member functions
#include <iostream>
class Dog
{
public:
void Speak()const { std::cout << "Woof!\n"; }
void Move() const { std::cout << "Walking to heel...\n"; }
void Eat() const { std::cout << "Gobbling food...\n"; }
void Growl() const { std::cout << "Grrrrr\n"; }
void Whimper() const { std::cout << "Whining noises...\n"; }
void RollOver() const { std::cout << "Rolling over...\n"; }
void PlayDead() const
{ std::cout << "Is this the end of Little Caeser?\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)
{
std::cout << "(0)Quit (1)Speak (2)Move (3)Eat (4)Growl";
std::cout << " (5)Whimper (6)Roll Over (7)Play Dead: ";
std::cin >> Method;
if (Method == 0)
{
fQuit = true;
break;
}
else
{
pDog = new Dog;
(pDog->*DogFunctions[Method-1])();
delete pDog;
}
}
return 0;
}
|