Jump to content

How to declare two classes that have a pointer n between?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
radel

radel

    Newbie

  • Members
  • Pip
  • 1 posts
Hello there,

Im new in C++ programming, and i ve some troubles that ive never seen in other languages.

I ve two classes, and each one has a pointer to the other:

class C1{
public: C2* pointerC2;

}

class C2{
public: C1* pointerC1;
}

Well, the compiler cannot parse it, i think its cause C2 is not visible for C1 in compiling time. In fact, for C1, C2 its not yet declare.

What can i do, please?

Thank you, and excuse my very bad english.

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Use a forward declaration.
[FONT="Fixedsys"][COLOR="Blue"]class C2; // forward declaration[/COLOR]

class C1{

public: C2* pointerC2;


}[COLOR="Red"];[/COLOR]


class C2{

public: C1* pointerC1;

}[COLOR="Red"];[/COLOR][/FONT]


#3
nutario

nutario

    Newbie

  • Members
  • PipPip
  • 23 posts

dcs said:

Use a forward declaration.
[FONT="Fixedsys"][COLOR="Blue"]class C2; // forward declaration[/COLOR]

class C1{

public: C2* pointerC2;


}[COLOR="Red"];[/COLOR]


class C2{

public: C1* pointerC1;

}[COLOR="Red"];[/COLOR][/FONT]

Edit: in both haeders!

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Ja - das denke ich!
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums