By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

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

P: n/a
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....
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"di boruman" <di****@freenet.de> wrote in message
news:47**************************@posting.google.c om...
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
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.