Another typedef issue
by maschelsea from LinuxQuestions.org on (#5C27W)
Here I am again proving that I have zero understanding of typedef. It should be easy to understand, right? I used to use aliases the same way that typedefs work, so I SHOULD be able to understand, Yet, I don't. This is listing14.11:
Code:michael@caitlyn 14 $ cat listing14.11.cpp
//Listing 14.11 - Array of pointers to member functions
//2020-12-24
#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 << "Grrrr...\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 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 = NULL;
int Method;
bool fQuit = false;
while(!fQuit)
{
std::cout << "(0)Quit, (1)Speak, (2)Move, (3)Eat, (4)Growl, (5)Whimper, (6)Roll Over, (7)Play Dead: ";
std::cin >> Method;
if (Method == 0) fQuit = true;
else
{
pDog = new Dog;
(pDog->*DogFunctions[Method - 1]);
delete pDog;
}
}
return 0;
}And here is the error listing:
Code:michael@caitlyn 14 $ g++ listing14.11.cpp -o listing14.11
listing14.11.cpp:17:19: error: expected ')' before '*' token
typedef void (Dog *PDF)() const;
^
listing14.11.cpp: In function 'int main()':
listing14.11.cpp:21:4: error: 'PDF' was not declared in this scope
PDF DogFunctions[MaxFuncs] =
^
listing14.11.cpp:43:11: error: 'DogFunctions' was not declared in this scope
(pDog->*DogFunctions[Method - 1]);
^The typedef above SHOULD be remapping (or aliasing) Dog to be void, so every time I want a void, I would say, "Dog". Is that right?


Code:michael@caitlyn 14 $ cat listing14.11.cpp
//Listing 14.11 - Array of pointers to member functions
//2020-12-24
#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 << "Grrrr...\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 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 = NULL;
int Method;
bool fQuit = false;
while(!fQuit)
{
std::cout << "(0)Quit, (1)Speak, (2)Move, (3)Eat, (4)Growl, (5)Whimper, (6)Roll Over, (7)Play Dead: ";
std::cin >> Method;
if (Method == 0) fQuit = true;
else
{
pDog = new Dog;
(pDog->*DogFunctions[Method - 1]);
delete pDog;
}
}
return 0;
}And here is the error listing:
Code:michael@caitlyn 14 $ g++ listing14.11.cpp -o listing14.11
listing14.11.cpp:17:19: error: expected ')' before '*' token
typedef void (Dog *PDF)() const;
^
listing14.11.cpp: In function 'int main()':
listing14.11.cpp:21:4: error: 'PDF' was not declared in this scope
PDF DogFunctions[MaxFuncs] =
^
listing14.11.cpp:43:11: error: 'DogFunctions' was not declared in this scope
(pDog->*DogFunctions[Method - 1]);
^The typedef above SHOULD be remapping (or aliasing) Dog to be void, so every time I want a void, I would say, "Dog". Is that right?