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

#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

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)
     }
}

did you try this: double bat::travel_time (double distance, terrain_type t)

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....

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:

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);
             }

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

double bat :: travel_time (double distance, terrain_type t)
{
       if (t == PLAIN)
       {
         distance = (distance/airspeed);
       }
       // and so on ...

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...

Ok, I think I've just about tried everything. I even went back to trying it out with the switch statements, except corrected:

switch (t)
      {
            case PLAIN: ((distance/landspeed)*1);
                        break;
            case HILL: ((distance/landspeed)*2);
                       break;
            case MOUNTAIN:((distance/landspeed)*3);
                       break;
     }

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,

"The trip would take 1.#QNAN hours!"

Any ideas why??

Look at post #5 and compare it with yours. You are missing the part to the left of the = symbol.

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....

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;
}

Thank you for your information. I see why it would be throwing away the equation, so then I will elaborate as much as I can as far as what I'm supposed to do with this function.

travel_time is a pure virtual function in the animal class. Derived classes from mammal will use the mammal travel_time function unless it is overrode (as in bat) and derived classes from bird will use the bird travel_time function unless overrode (as in penguin).

There are two general travel time functions. The function that uses flight (bird and bat) divides distance by airspeed. Terrain type is ignored. The function that uses ground level (mammal and penguin) divides distance by land speed and multiplies by a terrain factor. (1: PLAIN, 2: HILL, 3: MOUNTAIN).

I hope that makes things clearer in what I am finding trouble with. I hope not to be a pain...I truly am trying to learn this to the best of my efforts. Thank you for any input.

In the meantime, I am going to search for anything else that might be wrong in the implementation file...

The change I posted previously meets the requirements that you just posted in bold. You will have to make similar change to the mammal implementation that you posted.

I just saw that post now, there must have been a post overlap. I will try that now, thank you

AncientDragon, you're my hero! hah, thank you, it ran, compiled properly, solved. I really appreciate your help. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.