Syntax trouble with class inheriting "redefinition of class.."?

This is a discussion on Syntax trouble with class inheriting "redefinition of class.."? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm working on a basic program to practice working with inherited classes, parent - child and grandchild. The problem ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    Syntax trouble with class inheriting "redefinition of class.."?

    Hi, I'm working on a basic program to practice working with inherited classes, parent - child and grandchild. The problem is the book for my class is awful and any examples I find online are far to in depth for me to learn anything from them since my program is so basic. The basic idea is I have a Transport class which has 3 children - land, air, and water. There is also a class called military so some of the transports can have military attributes. I then have a file called tie3.h which has all the actual vehicles, cars, planes etc. The error I am getting is this:


    2 C:\Dev-Cpp\240c2\air.h:1, from tier3.h In file included from air.h:1, from tier3.h
    3 C:\Dev-Cpp\240c2\tier3.h:2, from main.cc from tier3.h:2, from main.cc
    4 C:\Dev-Cpp\240c2\transport.h redefinition of `class Transport'
    4 C:\Dev-Cpp\240c2\transport.h previous definition of `class Transport'
    5 C:\Dev-Cpp\240c2\air.h variable or field `output' declared void
    5 C:\Dev-Cpp\240c2\air.h expected `;' before '(' token
    6 C:\Dev-Cpp\240c2\air.h variable or field `input' declared void
    6 C:\Dev-Cpp\240c2\air.h expected `;' before '(' token
    13 C:\Dev-Cpp\240c2\air.h expected constructor, destructor, or type conversion before '(' token
    13 C:\Dev-Cpp\240c2\air.h expected `,' or `;' before '(' token


    Here is my tier3.h:
    Code:
    #include "military.h"
    #include "air.h"
    #include "land.h"
    #include "water.h"
    class Car:public Land{
          hp = 200;
          top_speed = 110;
          kpg = 30;
          cost = 15000;
    };
    
    class Truck:public Land{
          hp = 230;
          top_speed = 80;
          kpg = 22;
          cost = 17000;
    };
    
    class Bike:public Land{
          hp = 0;
          top_speed = 25;
          kpg = 0;
          cost = 100;
    };
    
    class Car:public Land, public Military{
          hp = 200;
          top_speed = 110;
          kpg = 30;
          guns = 2;
          intel_radius = 3;
          armor_weight = 150.75;
          cost = 35000;
    };
    
    //////////////////////////////////////
    
    class Glider:public Air{
          engine_hp = 0;
          wing_span = 20;
          propellars = 0;
          seats = 2;
          cost = 500;
    };
    
    class CropDuster:public Air{
          engine_hp = 240;
          wing_span = 40;
          propellars = 1;
          seats = 2;
          cost = 13000;
    };
    
    class SpyPlane:public Air, public Military{
          engine_hp = 900;
          wing_span = 100;
          propellars = 5;
          seats = 33;
          cost = 1200000;
    };
    
    /////////////////////////////////////////
    
    class JetSki:public Water{
          top_speed = 50;
          size = 12;
          cost = 3000;
    };
    
    class RiverBoat:public Water{
          top_speed = 30;
          size = 135;
          cost = 69000;
    };
    
    class Destroyer:public Water, public Military{
          top_speed = 80;
          size = 535;
          cost = 890000;
    };
    and here is air.h, water and land are basically the same:
    Code:
    #include "transport.h"
    class Air:public Transport{
          public:
                 //double cost(){return ((wing_span-propellars)/seats)*engine_hp;}
                 void output(iostream ofs);
                 void input(fstream ins);
          
          protected:
                  int engine_hp, wing_span, propellars, seats;
                  
    };
    
    Air::output(iostream outs){
       if(outs == cout){
              outs << "Engine HP: " << engine_hp << "Wing Span: " << wing_span << "Propellars: " << propellars << "Seats: " << seats << endl;
       else
           outs << engine_hp << endl << wing_span << endl << propellars << endl << seats << endl;
    }
    
    Air::input(fstream ins){
        getline(engine_hp, ins);
        getline(wing_span, ins);
        getline(propellars, ins);
        getline(seats, ins);
    }
    I think it has something to do with not having a constructor but my book says it will use a default constructor and I don't know where I would put it.

    Thanks
    -alex

  2. #2
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Show transport.h please, and maybe I can try and help.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You should really start much smaller and get one thing to work at a time. Unfortunately, the code in tier3.h is not close to correct. Try starting a new project and only implementing one derived class and getting that to compile.

    A couple things to note: Your output function should take an ostream&, not iostream. You cannot copy streams like that, and a reference (denoted by the '&') is a way to not make a copy but instead use the original. Similarly your input function should take an istream& as a parameter.

    In your derived classes, how you fix the code depends on how your base class is declared. If hp, top_speed, and the other variables are members of the base, then the base class should have a constructor that takes values to initialize them with. The derived class would then call the base class constructor with those default values.

    Make a really small program and make sure each change you make compiles before moving on to the next one.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    here is my transport.h
    Code:
    #include <iostream>
    #include <fstream>
    
    class Transport{
          public:
                 virtual void output() = 0;
                 virtual void input() = 0;
          
          protected:
                 double cost;           
                  
    };
    im going to start on some of Daved's suggestions and will let you know how it goes.

    as far as fixing my derived classes, those variables are not from the base class (transport.h) they are from the child of the base class (either air, land or water). in that case should those classes have the constructors?
    Last edited by aciarlillo; 10-01-2005 at 07:10 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Your header files also need include guards.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    include guards?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Yes. Your redefinition error is probably because of that. When you include a header, the preprocessor just places the text in the header file directly into the current file. If you do it multiple times (usually through some other files that also include that header), then the compiler is confused because the code defining the class is listed multiple times - so the class is redefined.

    A header guard generally looks like:
    Code:
    #ifndef TRANSPORT_H_
    #define TRANSPORT_H_
    
    // All your code
    
    #endif
    Check the FAQ or search the web for more details (if your book doesn't cover it - which it should).

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    alright yea i understand that my teacher just gave it to us as a diff name i guess. I still don't understand where I need the constructor or how to create the grandchild classes. Here is what I have revised:

    transport.h
    Code:
    #include <iostream>
    #include <fstream>
    
    #ifndef TRANSPORT_H_
    #define TRANSPORT_H_
    
    class Transport{
          public:
                 virtual void output() = 0;
                 virtual void input() = 0;
          
          protected:
                 double cost;           
                  
    };
    
    #endif
    land.h
    Code:
    #include "transport.h"
    
    #ifndef LAND_H_
    #define LAND_H_
    
    class Land:public Transport{
          public:
                 Land();
                 void output(ostream& outs);
                 void input(istream& ins);
          
          protected:
                  int hp, wheels, kpg; //horse power, wheels, kilometers per gallon
                  
    };
    
    #endif
    land.cc
    Code:
    #include "land.h"
    
    Land::output(ostream& outs){
       if(outs == cout){
              outs << "HP: " << hp << "Wheels: " << wheels << "KPG: " << kpg << endl;
       else
           outs << hp << endl << wheels << endl << kpg << endl;
    }
    
    Land::input(istream& ins){
        getline(hp, ins);
        getline(wheels, ins);
        getline(kpg, ins);
    }
    tier3.h (grandchild class)
    Code:
    #include "land.h"
    
    #ifndef TIER3_H_
    #define TIER3_H_
    
    class Car:public Land{
          Car(){hp = wheels = kpg = 0; cost = 0;}
    };
    right now these are the only files im using and in my main im doing this:
    Code:
    Transport *tptr;
    tptr = new Car;
    It now says Car::Car() is private within that context and that class Transport has no member named output. I'm going nuts because most of this code is straight from notes in class and it is not working at all. Any more help or shoves in the right direction are more than welcome, I'm going to be here for a while working on this.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You are missing the public: in the Car declaration to make the constructor public. It is private by default which is why it gets the error.

    When you overload a method in a derived class, the method parameters and return value should match between the base and derived classes. The output and input functions are different in Transport than they are in Land. They should be the same. (Technically, it is ok that the derived class doesn't have the virtual keyword, and the base class has the = 0 because it is pure virtual, but the parameters and return value should match).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Includes making me insane >.<
    By IceDane in forum C Programming
    Replies: 14
    Last Post: 04-14-2008, 10:24 AM
  3. Replies: 16
    Last Post: 11-10-2007, 02:51 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21