Article 59A0E [SOLVED] Horse and Pegasus 1

[SOLVED] Horse and Pegasus 1

by
maschelsea
from LinuxQuestions.org on (#59A0E)
I named my thread that because I have a feeling that this chapter will be problematic for me. I am working out of Sams Teach Yourself C++ For Linux in 21 Days. I'm on Day 13. I have typed in the listing for 13.2. When I tried to compile it, it erred greatly. I'm thinking that I have failed to observe one of C++'s unwritten rules that cause compilers to fail with no hint as to what the actual problem is. For starters, here's my error list:
Code:michael@caitlyn 13 $ g++ listing13.2.cpp -o listing13.2
listing13.2.cpp: In function 'int main()':
listing13.2.cpp:24:11: error: 'Ranch' was not declared in this scope
Horse* Ranch[NumberHorses];
^
listing13.2.cpp:25:11: error: 'pHorse' was not declared in this scope
Horse* pHorse;
^
listing13.2.cpp:32:16: error: 'Pegasus' does not name a type
pHorse = new Pegasus;
^
listing13.2.cpp:34:16: error: 'Horse' does not name a type
pHorse = new Horse;
^
listing13.2.cpp:40:16: error: 'pPeg' was not declared in this scope
Pegasus *pPeg = dynamic_cast <Pegasus*> (Ranch[i]);
^
listing13.2.cpp:40:37: error: 'Pegasus' does not name a type
Pegasus *pPeg = dynamic_cast <Pegasus*> (Ranch[i]);
^
listing13.2.cpp:40:44: error: expected '>' before '*' token
Pegasus *pPeg = dynamic_cast <Pegasus*> (Ranch[i]);
^
listing13.2.cpp:40:44: error: expected '(' before '*' token
listing13.2.cpp:40:45: error: expected primary-expression before '>' token
Pegasus *pPeg = dynamic_cast <Pegasus*> (Ranch[i]);
^
listing13.2.cpp:40:57: error: expected ')' before ';' token
Pegasus *pPeg = dynamic_cast <Pegasus*> (Ranch[i]);
^Here is the code listing:
Code:michael@caitlyn 13 $ cat listing13.2.cpp
//Listing 13.2 - Using dynamic_cast
//2020-10-20
#include <iostream>
enum TYPE { Horse, Pegasus };

class Horse
{
public:
virtual void Gallop() { std::cout << "Galloping...\n"; }
private:
int itsAge;
};

class Pegasus : public Horse
{
public:
virtual void Fly() { std::cout << "I can fly! I can fly! I can fly!\n"; }
};

const int NumberHorses = 5;

int main()
{
Horse* Ranch[NumberHorses];
Horse* pHorse;
int choice, i;
for(i = 0;i<NumberHorses;i++)
{
std::cout << "(1)Horse, (2)Pegasus: ";
std::cin >> choice;
if (choice == 2)
pHorse = new Pegasus;
else
pHorse = new Horse;
Ranch[i] = pHorse;
}
std::cout << "\n";
for (i = 0; i < NumberHorses; i++)
{
Pegasus *pPeg = dynamic_cast <Pegasus*> (Ranch[i]);
if (pPeg)
pPeg->Fly();
else
std::cout << "Just a horse.\n";
delete Ranch[i]
}
return 0;
}I find myself wondering why they included that enum just after the #include. They never reference TYPE.latest?d=yIl2AUoC8zA latest?i=4UBH-5BNTWA:aDZsYGqihbY:F7zBnMy latest?i=4UBH-5BNTWA:aDZsYGqihbY:V_sGLiP latest?d=qj6IDK7rITs latest?i=4UBH-5BNTWA:aDZsYGqihbY:gIN9vFw4UBH-5BNTWA
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments