| | |
C++ Inheritance problem with some syntax errors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 18
Reputation:
Solved Threads: 0
I am working on a code focusing on Inheritance in C++. I am getting some errors regarding the lines that read:
The others that begin with void penguin, mammal, and bird have the same error. The errors read:
--expected unqualified-id before "double"
--expected init declarator before "double"
--expected ',' or ';' before "double"
These seem like simple errors...but it's driving me nuts, I can't seem to find what is wrong. I'm hoping that maybe some fresh eyes could find it? It's also possible I could have something completely wrong in there all together...
Below I have information on what needs to be accomplished in the program. The cpp file and the .h file. Hope this is enough info.
animal class is the base. animal is inherited by mammal and bird. mammal is inherited by cow and bat. bird is inherited by hawk and penguin.
Derived classes from mammal uses travel_time function unless it is overrode (by bat). Derived classes from bird will use the travel_time function unless overrode (by penquin).
There is a chart where the numbers come from in the calculations in the void functions. The math isn't my problem. I think it's the declarators themselves. The problem is in the animal.h file. I just pasted the cpp file for reference to where things will go once program is ran.
I appreciate any input or advice.
cpp file
animal.h
void bat :: double travel_time (double distance, terrain_type t) (lines 56, 68, 113, 125 in animal.h file)The others that begin with void penguin, mammal, and bird have the same error. The errors read:
--expected unqualified-id before "double"
--expected init declarator before "double"
--expected ',' or ';' before "double"
These seem like simple errors...but it's driving me nuts, I can't seem to find what is wrong. I'm hoping that maybe some fresh eyes could find it? It's also possible I could have something completely wrong in there all together...
Below I have information on what needs to be accomplished in the program. The cpp file and the .h file. Hope this is enough info.
animal class is the base. animal is inherited by mammal and bird. mammal is inherited by cow and bat. bird is inherited by hawk and penguin.
Derived classes from mammal uses travel_time function unless it is overrode (by bat). Derived classes from bird will use the travel_time function unless overrode (by penquin).
There is a chart where the numbers come from in the calculations in the void functions. The math isn't my problem. I think it's the declarators themselves. The problem is in the animal.h file. I just pasted the cpp file for reference to where things will go once program is ran.
I appreciate any input or advice.
cpp file
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> using namespace std; #include "animal.h" char menu (); int main(int argc, char *argv[]) { animal* the_animal; // pointer to animal of a type selected by the user double distance; char animal; char command; double hours; terrain_type terrain; int terrain_number; // pick a default animal the_animal = new cow (); command = menu (); // continue executing until user enters a q to quit while (command != 'q') { // select appropriate directory operation based on command switch (command) { case 'a' : cout << "\nChoose an animal: "; cout << "\nh : hawk"; cout << "\np : penguin"; cout << "\nc : cow"; cout << "\nb : bat"; cout << "\n\nChoice: "; cin >> animal; // create the selected animal. Return old animal object // to free memory switch (animal) { case 'h' : delete the_animal; the_animal = new hawk (); break; case 'p' : delete the_animal; the_animal = new penguin (); break; case 'c' : delete the_animal; the_animal = new cow (); break; case 'b' : delete the_animal; the_animal = new bat (); break; default: cout << "\nIllegal choice, no animal set!"; } break; case 'p' : cout << "\nCurrent animal is:"; // print animal with public method the_animal -> print (); break; case 't' : cout << "\nEnter the distance: "; cin >> distance; cout << "Enter the terrain (0:PLAIN, 1:HILL, 2:MOUNTAIN): "; cin >> terrain_number; // change from integer input to terrain type terrain = (terrain_type) terrain_number; // compute travel time with public method hours = the_animal -> travel_time (distance, terrain); cout << "\nThe trip would take " << hours << " hours!"; break; default: cout << "\nlegal command entered!"; } // switch command = menu (); } // while system("PAUSE"); return EXIT_SUCCESS; } // prints the menu and returns user entered character command char menu () { char ch; cout << "\n\n Animal Menu"; cout << "\n\na: choose an animal"; cout << "\np: print current animal"; cout << "\nt: execute a trip"; cout << "\nq: quit from the program"; cout << "\n\nEnter command: "; cin >> ch; return ch; }
animal.h
C++ Syntax (Toggle Plain Text)
enum terrain_type {PLAIN, HILL, MOUNTAIN}; // animal class class animal { protected: char* kind; // name of animal double airspeed; // speed for animal flying double landspeed; // speed for animal walking public: void print (); // prints animal data // virtual function to compute travel time given a distance and // terrain type. Not defined in animal but in derived classes. virtual double travel_time (double distance, terrain_type t) = 0; }; // prints an animal's data (inherited by derived classes) void animal::print () { cout << "\n\nKind: " << kind; cout << "\nAir Speed: " << airspeed; cout << "\nLand Speed: " << landspeed; } class mammal : public animal { public: double travel_time (double distance, terrain_type t); }; class cow : public mammal { public: cow(); }; //constructor sets a default animal type, cow cow :: cow () { kind = "Cow"; airspeed = 0; landspeed = 3; } class bat : public mammal { public: bat(); double travel_time (double distance, terrain_type t); }; bat :: bat () { kind = "Bat"; airspeed = 15; landspeed = 1; } void bat :: double travel_time (double distance, terrain_type t) { switch (t) { case PLAIN: travel_time = (distance/airspeed) break; case HILL: travel_time = (distance/airspeed) break; case MOUNTAIN: travel_time = (distance/airspeed) } } void mammal :: double travel_time (double distance, terrain_type t) { switch (t) { case PLAIN: travel_time = ((distance/landspeed)*1) break; case HILL: travel_time = ((distance/landspeed)*2) break; case MOUNTAIN: travel_time = ((distance/landspeed)*3) } } class bird : public animal { public: double travel_time (double distance, terrain_type t); }; class hawk : public bird { public: hawk(); }; hawk :: hawk () { kind = "Hawk"; airspeed = 30; landspeed = 4; } class penguin : public bird { public: penguin(); double travel_time (double distance, terrain_type t); }; penguin :: penguin () { kind = "Penguin"; airspeed = 0; landspeed = 2; } void penguin :: double travel_time (double distance, terrain_type t) { switch (t) { case PLAIN: travel_time = ((distance/landspeed)*1) break; case HILL: travel_time = ((distance/landspeed)*2) break; case MOUNTAIN: travel_time = ((distance/landspeed)*3) } } void bird :: double travel_time (double distance, terrain_type t) { switch (t) { case PLAIN: travel_time = ((distance/landspeed)*1) break; case HILL: travel_time = ((distance/landspeed)*2) break; case MOUNTAIN: travel_time = ((distance/landspeed)*3) } }
•
•
Join Date: Feb 2009
Posts: 18
Reputation:
Solved Threads: 0
No I did not try that, that's exactly what I meant by fresh eyes! Thank you, no more errors on that.
However, now I've run into a new problem. It doesn't like my switch statements. I'm wondering if it would be better to use if statements instead? It's asking for the '&'. It is not call by reference so I can't use that option. Or maybe I'm missing something now in the switch statements that can still allow me to use them...
feel free, anyone, to shoot me any ideas if you have any. Thank you....
However, now I've run into a new problem. It doesn't like my switch statements. I'm wondering if it would be better to use if statements instead? It's asking for the '&'. It is not call by reference so I can't use that option. Or maybe I'm missing something now in the switch statements that can still allow me to use them...
feel free, anyone, to shoot me any ideas if you have any. Thank you....
•
•
Join Date: Feb 2009
Posts: 18
Reputation:
Solved Threads: 0
If I do in fact use the if statements instead of switch statements, I receive another error:
--expected primary expression before "=="
--expected primary expression before "t"
hmm.
here is what I tried:
--expected primary expression before "=="
--expected primary expression before "t"
hmm.
here is what I tried:
C++ Syntax (Toggle Plain Text)
double bat :: travel_time (double distance, terrain_type t) { if (terrain_type t == PLAIN) { distance = (distance/airspeed); } if (terrain_type t == HILL) { distance = (distance/airspeed); } if (terrain_type t == MOUNTAIN) { distance = (distance/airspeed); }
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
Or maybe I'm missing something now in the switch statements that can still allow me to use them...
void bird :: double travel_time (double distance, terrain_type t)
{
switch (t)
{
case PLAIN: travel_time = ((distance/landspeed)*1) ;
break;
case HILL: travel_time = ((distance/landspeed)*2) ;
break;
case MOUNTAIN: travel_time = ((distance/landspeed)*3) ;
// maybe add the final break too
break;
}
}The if-blocks won't work because you are trying to declare variables of terrain_type and compare them at once, i.e. use
C++ Syntax (Toggle Plain Text)
double bat :: travel_time (double distance, terrain_type t) { if (t == PLAIN) { distance = (distance/airspeed); } // and so on ...
•
•
Join Date: Feb 2009
Posts: 18
Reputation:
Solved Threads: 0
Great! Thank you, it runs now. I used the if statements instead of the switch statements.
My math is bit weird now, so now I have to find that little error I have in there. All answers come out to be "1.#QNAN" meaning it's obviously not calculating something right. It's not grabbing the data from landspeed and airspeed for some reason. I'm trying to figure that one out now...
My math is bit weird now, so now I have to find that little error I have in there. All answers come out to be "1.#QNAN" meaning it's obviously not calculating something right. It's not grabbing the data from landspeed and airspeed for some reason. I'm trying to figure that one out now...
•
•
Join Date: Feb 2009
Posts: 18
Reputation:
Solved Threads: 0
Ok, I think I've just about tried everything. I even went back to trying it out with the switch statements, except corrected:
Something in the math somewhere in the problem is not picking up on landspeed and airspeed numbers along with user entered distance. Just keeps giving me the same answer with everything,
Any ideas why??
C++ Syntax (Toggle Plain Text)
switch (t) { case PLAIN: ((distance/landspeed)*1); break; case HILL: ((distance/landspeed)*2); break; case MOUNTAIN:((distance/landspeed)*3); break; }
C++ Syntax (Toggle Plain Text)
"The trip would take 1.#QNAN hours!"
The post #5 is actually incorrect because it uses travel_time which is the name of the function as if it were a variable name. You need to create another double variable and return it, like this:
double bat::travel_time (double distance, terrain_type t)
{
double result = 0.0;
switch (t)
{
case PLAIN: result = (distance/airspeed);
break;
case HILL: result = (distance/airspeed);
break;
case MOUNTAIN: result = (distance/airspeed);
break;
}
return result;
} Last edited by Ancient Dragon; Mar 31st, 2009 at 1:33 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Errors with Char arrays
- Next Thread: Trouble when adding a library in borland C++(undefined symbol "fct_name" ...)
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






