Public Group

# inheritance redefinition problem

This topic is 4247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi people!!!!! How can I avoid inheritance redefinition? Let's imagine I've got the vehicle class, and to "sons", motorbike and car. I thought I could create vehicles from my main class just including vehicle.h, but I can. If I include vehicle.h I can't create a motorbike because it doesn't recognize it as a class/object/type. And I include motorbike.h I can create as motorbikes as I want, but I can't include car.h because of redefinition. The reason is motorbike includes have methods from vehicle.h as car.h I hope you have understood this, Anyone can help me please?

##### Share on other sites
I don't undestand one thing,

If I create:

Vehicle v;

why I can say after that:

v = car(supposed_attributes); ?????

Can anyone help me.

Refering to the redefinition part, I added in ALL the headers #ifndef, #define, #endif (I've read the article above -> thanks) And now I only have this two new erros:

c2011 class 'type' redefinition vehicle
c2504 base class undefined car

HELP

I've readed this but I undestood nothing

http://www.gamedev.net/community/forums/topic.asp?topic_id=296842&whichpage=1��

##### Share on other sites
Post up the header files for all three classes so we can see what you have so far.

##### Share on other sites
#pragma once did it!!!!!!!

I'm feeling so stupid right know

Anyone has any suggestion about my other question?

Can I create a vehicle ( Vehicle v) and then declare it as motorbike for example?

if I put Vehicle v; I must include Vehicle.h else if I want to create a motorbike I must include Motorbike.h, But I can create a Vehicle and then declare it as motorbike:

Vehicle v;
v= motorbike(...);

##### Share on other sites
I don't think it's possible, but you're better off instantializing a motorbike first anyway, rather than type-setting a vehicle as another class. If it's a matter of giving your players options, don't instantialize anything until they've made their choice.

Another thing that may help is to put your motorbike class definition and functions in the same header/cpp file as your vehicle definitions and functions, since motorbike is simply an derived vehicle class (or did you change that?). It will cut down on your #includes, if nothing more, and reduce the possibility of you forgetting and causing errors.

##### Share on other sites
well Now I'm not having any errors, I solved all with #pragma once

I'm not sure but I'm refering to some kind of polymorphism

##### Share on other sites
Quote:
 Original post by NcatDesignerI'm not sure but I'm refering to some kind of polymorphism

Polymorphism is achieved through pointers or references. Examples:
Vehicle* ptv1 = new motorbike(); // Created on the heap (remember to delete)// or the preferred way...void someFunction(Vehicle& v) // a motorbike can be passed to this function{  v.startEngine(); // overriden versions of these functions will be used if available  v.drive();}

1. 1
2. 2
3. 3
frob
20
4. 4
Rutin
20
5. 5

• 32
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632559
• Total Posts
3007080

×