![]() |
| | #1 |
| Registered User Join Date: Apr 2005
Posts: 29
| Syntax trouble with class inheriting "redefinition of class.."? 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;
};
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);
}
Thanks -alex |
| aciarlillo is offline | |
| | #2 |
| Registered Loser Join Date: Sep 2005 Location: United States
Posts: 33
| Show transport.h please, and maybe I can try and help. |
| nickodonnell is offline | |
| | #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 | |
| | #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;
};
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 | |
| | #5 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| Your header files also need include guards. |
| Daved is offline | |
| | #6 |
| Registered User Join Date: Apr 2005
Posts: 29
| include guards? |
| aciarlillo is offline | |
| | #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 |
| Daved is offline | |
| | #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
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
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);
}
Code: #include "land.h"
#ifndef TIER3_H_
#define TIER3_H_
class Car:public Land{
Car(){hp = wheels = kpg = 0; cost = 0;}
};
Code: Transport *tptr; tptr = new Car; |
| aciarlillo is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |