Bi-directional link between two class

R

Robert

Hi all,

I encounter a problem: I want to define two class, A has a point to B
and B also has a point to A. Something like a bi-directional link list.
But It has compile error.

//---code-------
class A
{
...
B* b;
}

class B
{
...
A* a;
}
//--------------
The error message is B is a unknown type.

Best regards,
Robert
 
M

Matthias Kaeppler

Robert said:
The error message is B is a unknown type.

That's because at the point of declaration of A::b, class B is unkown.
You have to forward-declare it:

//---code-------
class B;

class A
{
...
B* b;
};

class B
{
...
A* a;
};
//--------------
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top