how to create a member, which knows about the object it was created in

D

di boruman

Hi,

I would like to let a member a of class A know about the object it
was created in:

A a(this);

'this' would be stored in a member of A, for example

B* b;

for this purpose I included A.h in B.h and B.h in A.h (without
problems) but when I try to declare the 'B* b;' I get a strange
error (error C2501 - the error message is in Japanese
(I use the Japanese version of Visual C++ .NET) and
says something like "even if the identifier was declared,
his shape is unspecified).

here is the miminum code producing the error:

int main(int argc, char* argv[])
{
return 0;
}

class A
{
public:
B* b; // if I comment this line out everything works fine
A(void) {};
~A(void) {};
};

class B
{
public:
// A* a;
B(void) {};
~B(void) {};
};

Actually I would like to also let B know about A, see the comment in
class B...

Is there anybody who can tell me, why this is not possible??
Or even better how it could be possible???
(or why it makes sense to forbid things like this - for the case
it doesn't make sense...)

any help??
Thanks a lot :), di

PS: The context of this problem is the following:
I try to implement a little Genetic Algorithm and have a
class 'Individual' with two members: 'Chromosome' and 'Phenotype'.
I want the Phenotype to be able to adress the corresponding
cromosome. As the individual knows about the
chromosome, the easyest way would be to let the phenotype know
about the individual it is belonging to....
 
J

John Harrison

di boruman said:
Hi,

I would like to let a member a of class A know about the object it
was created in:

A a(this);

'this' would be stored in a member of A, for example

B* b;

for this purpose I included A.h in B.h and B.h in A.h (without
problems) but when I try to declare the 'B* b;' I get a strange
error (error C2501 - the error message is in Japanese
(I use the Japanese version of Visual C++ .NET) and
says something like "even if the identifier was declared,
his shape is unspecified).

here is the miminum code producing the error:

int main(int argc, char* argv[])
{
return 0;
}

Add this here, its called a forward declaration.

class B;
class A
{
public:
B* b; // if I comment this line out everything works fine
A(void) {};
~A(void) {};
};

class B
{
public:
// A* a;
B(void) {};
~B(void) {};
};

Actually I would like to also let B know about A, see the comment in
class B...

Is there anybody who can tell me, why this is not possible??
Or even better how it could be possible???
(or why it makes sense to forbid things like this - for the case
it doesn't make sense...)

any help??
Thanks a lot :), di

john
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top