C++ Inheritance problem with some syntax errors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 18
Reputation: danielle23 is an unknown quantity at this point 
Solved Threads: 0
danielle23 danielle23 is offline Offline
Newbie Poster

C++ Inheritance problem with some syntax errors

 
0
  #1
Mar 30th, 2009
I am working on a code focusing on Inheritance in C++. I am getting some errors regarding the lines that read:
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
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. #include "animal.h"
  7.  
  8. char menu ();
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. animal* the_animal; // pointer to animal of a type selected by the user
  13. double distance;
  14. char animal;
  15. char command;
  16. double hours;
  17. terrain_type terrain;
  18. int terrain_number;
  19.  
  20. // pick a default animal
  21. the_animal = new cow ();
  22.  
  23. command = menu ();
  24. // continue executing until user enters a q to quit
  25. while (command != 'q')
  26. {
  27. // select appropriate directory operation based on command
  28. switch (command)
  29. {
  30. case 'a' : cout << "\nChoose an animal: ";
  31. cout << "\nh : hawk";
  32. cout << "\np : penguin";
  33. cout << "\nc : cow";
  34. cout << "\nb : bat";
  35. cout << "\n\nChoice: ";
  36. cin >> animal;
  37. // create the selected animal. Return old animal object
  38. // to free memory
  39. switch (animal)
  40. {
  41. case 'h' : delete the_animal;
  42. the_animal = new hawk ();
  43. break;
  44. case 'p' : delete the_animal;
  45. the_animal = new penguin ();
  46. break;
  47. case 'c' : delete the_animal;
  48. the_animal = new cow ();
  49. break;
  50. case 'b' : delete the_animal;
  51. the_animal = new bat ();
  52. break;
  53. default: cout << "\nIllegal choice, no animal set!";
  54. }
  55. break;
  56. case 'p' : cout << "\nCurrent animal is:";
  57. // print animal with public method
  58. the_animal -> print ();
  59. break;
  60. case 't' : cout << "\nEnter the distance: ";
  61. cin >> distance;
  62. cout << "Enter the terrain (0:PLAIN, 1:HILL, 2:MOUNTAIN): ";
  63. cin >> terrain_number;
  64. // change from integer input to terrain type
  65. terrain = (terrain_type) terrain_number;
  66. // compute travel time with public method
  67. hours = the_animal -> travel_time (distance, terrain);
  68. cout << "\nThe trip would take " << hours << " hours!";
  69. break;
  70. default: cout << "\nlegal command entered!";
  71. } // switch
  72. command = menu ();
  73. } // while
  74.  
  75. system("PAUSE");
  76. return EXIT_SUCCESS;
  77. }
  78.  
  79. // prints the menu and returns user entered character command
  80. char menu ()
  81. {
  82. char ch;
  83.  
  84. cout << "\n\n Animal Menu";
  85. cout << "\n\na: choose an animal";
  86. cout << "\np: print current animal";
  87. cout << "\nt: execute a trip";
  88. cout << "\nq: quit from the program";
  89. cout << "\n\nEnter command: ";
  90. cin >> ch;
  91. return ch;
  92. }

animal.h
  1. enum terrain_type {PLAIN, HILL, MOUNTAIN};
  2.  
  3. // animal class
  4. class animal
  5. {
  6. protected:
  7. char* kind; // name of animal
  8. double airspeed; // speed for animal flying
  9. double landspeed; // speed for animal walking
  10. public:
  11. void print (); // prints animal data
  12. // virtual function to compute travel time given a distance and
  13. // terrain type. Not defined in animal but in derived classes.
  14. virtual double travel_time (double distance, terrain_type t) = 0;
  15. };
  16.  
  17. // prints an animal's data (inherited by derived classes)
  18. void animal::print ()
  19. {
  20. cout << "\n\nKind: " << kind;
  21. cout << "\nAir Speed: " << airspeed;
  22. cout << "\nLand Speed: " << landspeed;
  23. }
  24.  
  25. class mammal : public animal
  26. {
  27. public:
  28. double travel_time (double distance, terrain_type t);
  29. };
  30. class cow : public mammal
  31. {
  32. public:
  33. cow();
  34. };
  35. //constructor sets a default animal type, cow
  36. cow :: cow ()
  37. {
  38. kind = "Cow";
  39. airspeed = 0;
  40. landspeed = 3;
  41. }
  42. class bat : public mammal
  43. {
  44. public:
  45. bat();
  46. double travel_time (double distance, terrain_type t);
  47. };
  48.  
  49. bat :: bat ()
  50. {
  51. kind = "Bat";
  52. airspeed = 15;
  53. landspeed = 1;
  54. }
  55.  
  56. void bat :: double travel_time (double distance, terrain_type t)
  57. {
  58. switch (t)
  59. {
  60. case PLAIN: travel_time = (distance/airspeed)
  61. break;
  62. case HILL: travel_time = (distance/airspeed)
  63. break;
  64. case MOUNTAIN: travel_time = (distance/airspeed)
  65. }
  66. }
  67.  
  68. void mammal :: double travel_time (double distance, terrain_type t)
  69. {
  70. switch (t)
  71. {
  72. case PLAIN: travel_time = ((distance/landspeed)*1)
  73. break;
  74. case HILL: travel_time = ((distance/landspeed)*2)
  75. break;
  76. case MOUNTAIN: travel_time = ((distance/landspeed)*3)
  77. }
  78. }
  79.  
  80. class bird : public animal
  81. {
  82. public:
  83. double travel_time (double distance, terrain_type t);
  84. };
  85.  
  86. class hawk : public bird
  87. {
  88. public:
  89. hawk();
  90. };
  91.  
  92. hawk :: hawk ()
  93. {
  94. kind = "Hawk";
  95. airspeed = 30;
  96. landspeed = 4;
  97. }
  98. class penguin : public bird
  99. {
  100. public:
  101. penguin();
  102. double travel_time (double distance, terrain_type t);
  103.  
  104. };
  105.  
  106. penguin :: penguin ()
  107. {
  108. kind = "Penguin";
  109. airspeed = 0;
  110. landspeed = 2;
  111. }
  112.  
  113. void penguin :: double travel_time (double distance, terrain_type t)
  114. {
  115. switch (t)
  116. {
  117. case PLAIN: travel_time = ((distance/landspeed)*1)
  118. break;
  119. case HILL: travel_time = ((distance/landspeed)*2)
  120. break;
  121. case MOUNTAIN: travel_time = ((distance/landspeed)*3)
  122. }
  123. }
  124.  
  125. void bird :: double travel_time (double distance, terrain_type t)
  126. {
  127. switch (t)
  128. {
  129. case PLAIN: travel_time = ((distance/landspeed)*1)
  130. break;
  131. case HILL: travel_time = ((distance/landspeed)*2)
  132. break;
  133. case MOUNTAIN: travel_time = ((distance/landspeed)*3)
  134. }
  135. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Inheritance problem with some syntax errors

 
0
  #2
Mar 30th, 2009
did you try this: double bat::travel_time (double distance, terrain_type t)
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: danielle23 is an unknown quantity at this point 
Solved Threads: 0
danielle23 danielle23 is offline Offline
Newbie Poster

Re: C++ Inheritance problem with some syntax errors

 
0
  #3
Mar 30th, 2009
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....
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: danielle23 is an unknown quantity at this point 
Solved Threads: 0
danielle23 danielle23 is offline Offline
Newbie Poster

Re: C++ Inheritance problem with some syntax errors

 
0
  #4
Mar 30th, 2009
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:
  1. double bat :: travel_time (double distance, terrain_type t)
  2. {
  3. if (terrain_type t == PLAIN)
  4. {
  5. distance = (distance/airspeed);
  6. }
  7. if (terrain_type t == HILL)
  8. {
  9. distance = (distance/airspeed);
  10. }
  11. if (terrain_type t == MOUNTAIN)
  12. {
  13. distance = (distance/airspeed);
  14. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: C++ Inheritance problem with some syntax errors

 
0
  #5
Mar 30th, 2009
Originally Posted by danielle23 View Post
Or maybe I'm missing something now in the switch statements that can still allow me to use them...
Yes, you are missing semicolons ...
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
  1. double bat :: travel_time (double distance, terrain_type t)
  2. {
  3. if (t == PLAIN)
  4. {
  5. distance = (distance/airspeed);
  6. }
  7. // and so on ...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: danielle23 is an unknown quantity at this point 
Solved Threads: 0
danielle23 danielle23 is offline Offline
Newbie Poster

Re: C++ Inheritance problem with some syntax errors

 
0
  #6
Mar 30th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: danielle23 is an unknown quantity at this point 
Solved Threads: 0
danielle23 danielle23 is offline Offline
Newbie Poster

Re: C++ Inheritance problem with some syntax errors

 
0
  #7
Mar 30th, 2009
Ok, I think I've just about tried everything. I even went back to trying it out with the switch statements, except corrected:
  1. switch (t)
  2. {
  3. case PLAIN: ((distance/landspeed)*1);
  4. break;
  5. case HILL: ((distance/landspeed)*2);
  6. break;
  7. case MOUNTAIN:((distance/landspeed)*3);
  8. break;
  9. }
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,
  1. "The trip would take 1.#QNAN hours!"
Any ideas why??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Inheritance problem with some syntax errors

 
0
  #8
Mar 31st, 2009
Look at post #5 and compare it with yours. You are missing the part to the left of the = symbol.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: danielle23 is an unknown quantity at this point 
Solved Threads: 0
danielle23 danielle23 is offline Offline
Newbie Poster

Re: C++ Inheritance problem with some syntax errors

 
0
  #9
Mar 31st, 2009
I took travel_time off because errors came up asking if I was missing the "&". It's not supposed to be a call by reference, so when I took travel_time out, the program ran and I thought that's how it was supposed to work...until the answer kept coming up as a no number

hmmm....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Inheritance problem with some syntax errors

 
0
  #10
Mar 31st, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC