I don't know how to deal with this Complie Error.

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

I don't know how to deal with this Complie Error.

Post by ImLazy » Sun Aug 21, 2005 7:52 am

Code: Select all

class A{
public:
  B b;
};

class B{
public:
  A a;
};

int main(){
  A a;
  B b;
  return 0;
}
I really want to use an object of B in class A, and also an object of A in class B. How can I do that?
I stay home. Don't call me out.

User avatar
Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Re: I don't know how to deal with this Complie Error.

Post by Martin Macko » Sun Aug 21, 2005 9:27 am

ImLazy wrote:I really want to use an object of B in class A, and also an object of A in class B. How can I do that?
How do you expect the classes would look like? What would be their size?
How can you possibly put a red cup into a blue cup while having inserted the blue one into the red one?

Probably pointers will do the work:

Code: Select all

class B;

class A{
public:
  B *b;
};

class B{
public:
  A *a;
};

int main(){
  A a;
  B b;
  return 0;
}

User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy » Mon Aug 22, 2005 10:14 am

You are right Martin Macko, I can improve the structure of my code and prevent this error. Thank you.
I stay home. Don't call me out.

Post Reply

Return to “C++”