two classes #including each other

S

stefven blonqhern

hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:

i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:

class MyClass {

void MyFunction{
aShape->DoSomethingToShape(this)
}

private:

Shape* aShape;
}

/////////////

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type
'struct MyClass' **
}

I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.

Is there a better way?

thanks, stefven.
 
N

Neelesh Bodas

hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:

i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:

class MyClass {

void MyFunction{
aShape->DoSomethingToShape(this)

}

private:

Shape* aShape;

}

/////////////

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type

myClass needs to be defined before its members are used.
'struct MyClass' **

}

I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.

You can do something like this: (just an example):

//Myclass.h
class Myclass
{
public:
void foo();
};


//Myclass.cpp
#include "Myclass.h"
void Myclass::foo()
{
...
}


//shape.h
class Myclass;
class Shape
{
public:
void DoSomethingToShape(Myclass*);
};


//shape.cpp
#include "shape.h"
#include "myclass.h"

void Shape::DoSomethingToShape(Myclass* aClass)
{
aClass->foo();
}


-N
 
V

Victor Bazarov

stefven said:
hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:

i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:

class MyClass {

void MyFunction{
aShape->DoSomethingToShape(this)
}

private:

Shape* aShape;
}

/////////////

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type
'struct MyClass' **
}

Pull this function's definition out of the class 'Shape' and
I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.

Is there a better way?

Usually, yes. The Java habit of having all functions implemented in
the class definition is not the best to follow when C++ is concerned.

V
 
S

stefven blonqhern

okay, thanks for the replies.. naturally i forgot to mention that i'm
using public inheritance. Shape being the base class and all member
functions are virtual.

looks like i have some thinking to do, thanks,
stefven.
 
S

stefven blonqhern

Is there a better way?
Usually, yes. The Java habit of having all functions implemented in
the class definition is not the best to follow when C++ is concerned.

so really i should be separating the interface and the
implementation. Java? never heard of it!

Stefven
 
V

Victor Bazarov

stefven said:
so really i should be separating the interface and the
implementation. Java? never heard of it!

Interface and implemenatation are concepts, they are separate by
nature. You should be separating declarations and definitions.

V
 
M

Martijn van Buul

* Victor Bazarov:
You should be separating declarations and definitions.

Simplifying things again, aren't we? There are various reasons why you
wouldn't want to seperate the definitions, or even *can't* seperate the
definitions. Function inlining comes to mind; in order for this to work
you *NEED* the declaration and the definition to be in the same place.
Templates come to mind, as well.
 
P

Pete Becker

* Victor Bazarov:

Simplifying things again, aren't we? There are various reasons why you
wouldn't want to seperate the definitions, or even *can't* seperate the
definitions. Function inlining comes to mind; in order for this to work
you *NEED* the declaration and the definition to be in the same place.
Templates come to mind, as well.

Well, "in the same place" in the sense that both have to be present if
the function is called. That doesn't mean the function's definition has
to be inside the class definition. It can come later. It also doesn't
mean that the function's definition has to be in the same source file
as the class definition. Some people put them in separate files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top