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

Discussion in 'C++' started by di boruman, Aug 14, 2003.

  1. di boruman

    di boruman Guest

    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....
    di boruman, Aug 14, 2003
    #1
    1. Advertising

  2. "di boruman" <> wrote in message
    news:...
    > 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
    John Harrison, Aug 14, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amit
    Replies:
    3
    Views:
    421
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Dec 9, 2006
  2. lovecreatesbeauty
    Replies:
    43
    Views:
    1,297
    Keith Thompson
    Feb 6, 2006
  3. lovecreatesbeauty
    Replies:
    2
    Views:
    577
  4. Reckoner
    Replies:
    8
    Views:
    235
  5. casey
    Replies:
    8
    Views:
    124
    Thomas 'PointedEars' Lahn
    Oct 23, 2005
Loading...

Share This Page