C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-01-2005, 05:40 PM   #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
aciarlillo is offline   Reply With Quote
Old 10-01-2005, 07:00 PM   #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.
nickodonnell is offline   Reply With Quote
Old 10-01-2005, 07:00 PM   #3
Registered User
 
Join Date: Jan 2005
Posts: 7,137
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.
Daved is offline   Reply With Quote
Old 10-01-2005, 07:07 PM   #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.
aciarlillo is offline   Reply With Quote
Old 10-01-2005, 07:09 PM   #5
Registered User
 
Join Date: Jan 2005
Posts: 7,137
Your header files also need include guards.
Daved is offline   Reply With Quote
Old 10-01-2005, 07:13 PM   #6
Registered User
 
Join Date: Apr 2005
Posts: 29
include guards?
aciarlillo is offline   Reply With Quote
Old 10-01-2005, 07:18 PM   #7
Registered User
 
Join Date: Jan 2005
Posts: 7,137
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).
Daved is offline   Reply With Quote
Old 10-01-2005, 07:31 PM   #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.
aciarlillo is offline   Reply With Quote
Old 10-02-2005, 12:27 AM   #9
Registered User
 
Join Date: Jan 2005
Posts: 7,137
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).
Daved is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing some code, lots of errors... Sparrowhawk C Programming 48 12-15-2008 04:09 AM
Includes making me insane >.< IceDane C Programming 14 04-14-2008 10:24 AM
Trouble with overloading operators in a class with composition relationship clegs C++ Programming 16 11-10-2007 03:51 PM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
gcc problem bjdea1 Linux Programming 13 04-29-2002 06:51 PM


All times are GMT -6. The time now is 08:56 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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