How to acces previosly created classes from a sub class?

Pages: 12
Well the problem sems to be quite simple, but I'm a bit lost in code right now.

Here's some example code:

myclass.h
1
2
3
4
5
6
7
include "subclass.h"
class MyClass
{
   public:
     float something;
     SubClass mySubClass;
}


subclass.h
1
2
3
4
5
6
7
8
9
10
11
class SubClass
{
   public:
     MyClass mainClass; // how to do this correctly?
     void DoSomething();
}

void SubClass::DoSomething()
{
   mainClass.something = 1234.5678f;
}
class SubClass : public MyClass {
When you say int foo; or MyClass bar; you are creating a new object.

The code you have posted will make it so that MyClass contains a SubClass object... but then the SubClass object will contain another MyClass object, which contains another SubClass object, etc, etc, etc.

So clearly that is not what you want.

Instead, you want to access an existing object that was created elsewhere. For this, you can use a pointer. Pointers are not objects themselves, but instead "point to" an existing object.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Subclass
{
    MyClass* owner;  // <- The * makes it a pointer

    //...
}

void SubClass::DoSomething()
{
   // now, assuming 'owner' has been initialized properly,
   //   you can access your owner's 'something' member like this:
   owner->something = 1234.5678f;
}



The MyClass object will then need to tell the SubClass who the owner is (ie: it will need to provide that pointer. You can probably do this in the MyClass constructor:

1
2
3
4
MyClass::MyClass()
{
    mySubClass.owner = this;  // tell the sub class that 'this' is the owner
}




EDIT:

Ninja'd by EssGeEich. Inheritance is another way to go, but has different implications. Are you trying to derive a class? Or is this an owner/owned relationship?

To clarify: Inheritance replies an "is a" relationship. The parent class represents a generic type, and the derived class represents a more specialized version of that type.

For example:

Parent class Vehicle might have derived classes Car, Boat, Airplane, etc.
Parent class Dog might have derived classes Poodle, Pug, etc.


On the other hand, ownership implies that one object owns another... like the smaller class is a part in a larger machine.

For example:

Owner class Car might have owned classes Engine, Windshield
Last edited on
To the OP: The term "subclass" is used to describe a class that inherits from a base class. Class B is called a subclass of class A if class B inherits from class A:

1
2
3
4
class B: public A
{
  // ...
}


Is this actually what you meant? Or did you mean something different?
Thans for the reply, but I tryed this and I got a whole lot of class duplication errors now, heres a new example with the method provided by Disch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Engine.h"
#include "Gearbox.h"
class CarClass
{
	public:
		EngineClass engine;
		GearboxClass gearbox;
		CarClass(void);
    //...
}

CarClass::CarClass()
{
    gearbox.car = this;  // tell the sub class that 'this' is the owner
    //...
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Car.h"
class Gearbox
{
	public:
		CarClass* car;  // <- The * makes it a pointer
		void DoSomething();
    //...
}

Gearbox::DoSomething()
{
	 float rpm = car->engine.GetRPM();
	 //...
}


Errors like this:
...error C2011: 'CarEngine' : 'class' type redefinition
Last edited on
http://www.cplusplus.com/forum/articles/10627/ (4)

Also, you shouldn't have function definitions in .h (unless they are inline or template)
Looks like you might have a combination of circular inclusion + lack of header guards.

Article you might want to check out:
http://www.cplusplus.com/forum/articles/10627/#msg49679

Long story short there are 2 problems:

1) You need to guard your headers so that when you include them multiple times, all but the first get ignored. This can be done like so:

1
2
3
4
5
6
7
8
9
// in car.h
#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED

//
// put the contents of your header here
//

#endif 



#2) If car.h is including engine.h, then engine.h cannot include car.h. That'll create a circular include problem that will confuse the compiler. Instead...

- Include headers where you need to create objects. IE: car.h should include engine.h because the car needs a full engine object.

- Forward declare classes when you only need a pointer. IE: engine.h should not include car.h, but instead should forward declare the Car class:


Example (assume headers are guarded as described above):

1
2
3
4
5
6
7
8
// car.h

#include "engine.h"

class Car
{
    Engine engine;
};

1
2
3
4
5
6
7
8
// engine.h

class Car;  // just forward declare it... don't include the header

class Engine
{
    Car* car;
};
Last edited on
It seemed to work so far, but when I try to access the new pointer to Car

Gave me a error like this:
error C2027: use of undefined type 'Car'
You'll need to show us your code. Very few people here are telepaths.
Since you are only forward declaring Car in the Engine.h header... Engine.cpp will need to #include car.h.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CARCLASS_H_INCLUDED
#define CARCLASS_H_INCLUDED
#include "Engine.h"
#include "Gearbox.h"
class CarClass
{
	public:
		EngineClass engine;
		GearboxClass gearbox;
		CarClass(void);
    //...
}

CarClass::CarClass()
{
    //...
}
#endif  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

class CarClass;

class Gearbox
{
	CarClass* car;  // <- The * makes it a pointer
	public:
		void DoSomething();
    //...
}

Gearbox::DoSomething()
{
	 float rpm = car->engine.GetRPM();// gives error C2027: use of undefined type 'CarClass'
	 //...
}
Inlining functions in a header is tricky (not to mention it won't work here anyway since you are missing the inline keyword and it's not implicitly inlined)

Move Gearbox::DoSomething to a cpp file. That cpp file will need to include both gearbox.h and car.h, since it is using both those classes.
I have the functions in .cpp file now and I have the both gearbox.h and car.h included in the .cpp file aswell. But it still didnt work.

As I understand I have to inline the function DoSomething() ?
How should I do this?
As I understand I have to inline the function DoSomething() ?


No you do not have to do that. Forget I said anything about inlining, I probably just confused you.

I have the functions in .cpp file now and I have the both gearbox.h and car.h included in the .cpp file aswell. But it still didnt work.


If you're doing what you describe here it should be working. I'll have to see the code and error messages to diagnose further.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CARCLASS_H_INCLUDED
#define CARCLASS_H_INCLUDED
#include "Engine.h"
#include "Gearbox.h"
class CarClass
{
	public:
		EngineClass engine;
		GearboxClass gearbox;
		CarClass(void);
    //...
}

CarClass::CarClass()
{
    //...
}
#endif  


1
2
3
4
5
6
7
8
9
10

class CarClass;

class Gearbox
{
	CarClass* car;  // <- The * makes it a pointer
	public:
		void DoSomething();
    //...
}


.cpp
1
2
3
4
5
6
7
8
#include "Gearbox.h"
#include "Car.h"

Gearbox::DoSomething()
{
	 float rpm = car->engine.GetRPM();
	 //...
}


Errors I get
1
2
3
4
5
6
7
8
9
10
11
...some errors with a class that seems to be declared twice, Ill take a look on where it's beeing declared and try to guard the header

1>c:\users\ravel\documents\visual studio 2010\projects\mphysics\mphysics\src\CarGearbox.h(10): error C2011: 'CarGearbox' : 'class' type redefinition
1>          c:\users\ravel\documents\visual studio 2010\projects\mphysics\mphysics\src\CarGearbox.h(10) : see declaration of 'CarGearbox'
1>c:\users\ravel\documents\visual studio 2010\projects\mphysics\mphysics\src\CarClass.h(33): error C2079: 'CarClass::gearbox' uses undefined class 'CarGearbox'
1>src\CarGearbox.cpp(7): error C2027: use of undefined type 'CarGearbox'
1>          c:\users\ravel\documents\visual studio 2010\projects\mphysics\mphysics\src\CarGearbox.h(10) : see declaration of 'CarGearbox'
1>src\CarGearbox.cpp(7): error C2062: type 'void' unexpected
1>src\CarGearbox.cpp(8): error C2143: syntax error : missing ';' before '{'

...and the errors go on, but its a chain caused by one error, so I think it wouldnt be informative 
Last edited on
> GearboxClass gearbox;
> class Gearbox
> 'CarClass::gearbox' uses undefined class 'CarGearbox'
I don't think is too much to ask for you to provide the actual code that correspond to the error messages.

Also, you're missing include guards in `Gearbox.h'


And again, move your definition of the constructor to a source file, or make it inline.
This is how it should look like:

Class_A.h
1
2
3
4
5
6
class Class_A {
protected:
    void Class_A_Some_Function();
// Protected: Class_A and Subclasses can access this functions.
// No one else can.
};


Class_A.cpp
1
2
3
4
#include "Class_A.h"
void Class_A::Class_A_Some_Function() {
    // ...
}


Class_B.h
1
2
3
4
5
6
7
8
#include "Class_A.h"
class Class_B : public Class_A {
public:
    void Class_B_Some_Function();
// will call Class_A_Some_Function
// even if it's protected, because Class_B derives from Class_A
// and as stated above, is called a subclass of Class_A.
};


Class_B.cpp
1
2
3
4
#include "Class_B.h"
void Class_B::Class_B_Some_Function() {
    Class_A_Some_Function(); // easy as that
}


Hope it's somehow clear.

But anyways:
Variables and function names must go in .h files.
Function bodies must go in .cpp files.
I added the Guards to gearbox and it compiled without errors. Now I got a new issue, the dll I compiled crashed while trying to run it.

Any alternative ways to achieve this?

As for the code, its huge and I'm not going to post the whole code. I'm posting the piece of it wich is causing the error, since it's now compiling I just need to know the alternative way of achieving the same...

Heres the code that compiled:

carclass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef CARCLASS_H_INCLUDED
#define CARCLASS_H_INCLUDED
#include "Engine.h"
#include "Gearbox.h"
class CarClass
{
	public:
		EngineClass engine;
		GearboxClass gearbox;
		CarClass(void);
    //...
}
#endif  



cargearbox.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef GARGEARBOX_H_INCLUDED
#define GARGEARBOX_H_INCLUDED

#include <math.h>
#include <vector>
#include "monoarray.h"

class CarClass;

using namespace std;

class CarGearbox
{
    public:
		CarGearbox(void);
		~CarGearbox(void);
		
		CarClass* car;  // <- The * makes it a pointer

    //...
}
#endif  


cargearbox.cpp
1
2
3
4
5
6
7
8
#include "Gearbox.h"
#include "Car.h"

Gearbox::DoSomething()
{
	 float rpm = car->engine.GetRPM();
	 //...
}


wil try the method provided by EssGeEich now
Last edited on
Tryed the other mehtod aswell, and I got that the base class is undefined...

error C2504: 'CarClass' : base class undefined
I added the Guards to gearbox and it compiled without errors. Now I got a new issue, the dll I compiled crashed while trying to run it.


This is a completely separate problem.

If you are running from a debugger it should tell you exactly which line it is crashing on. Do you know which line it is?

Do you get any kind of error message when it crashes? Like "access violation" or anything?
Pages: 12