Connecting Tech Pros Worldwide Forums | Help | Site Map

Classses of each other

Jon Slaughter
Guest
 
Posts: n/a
#1: Jul 23 '05
I'm trying do to the following

class Edge
{
Node *N;

....
}


class Node
{
Edge *E;

...
}


But ofcourse I get an error that I assume is because of scope. Is there any
way to get something like this to work?

Thanks,
Jon



Sep
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Classses of each other


Forward declare class Node so that the parser knows about it when
declaring N.

class Node;

class Edge
{
....

Jon Slaughter
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Classses of each other



"Sep" <Sep102@gmail.com> wrote in message
news:1110661293.121548.12820@o13g2000cwo.googlegro ups.com...[color=blue]
> Forward declare class Node so that the parser knows about it when
> declaring N.
>
> class Node;
>
> class Edge
> {
> ...
>[/color]

I tried that... But it didn't work(I added a public member a into Node and
tried to access it in edge but I get an error... I will try again and see,
maybe I mised something.


Sep
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Classses of each other


Well, if that's your exact code, then you're missing the semicolons
after the ending curly brackets for the classes which are not optional,
that would give you a syntax error.

Jon Slaughter
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Classses of each other



"Sep" <Sep102@gmail.com> wrote in message
news:1110661293.121548.12820@o13g2000cwo.googlegro ups.com...[color=blue]
> Forward declare class Node so that the parser knows about it when
> declaring N.
>
> class Node;
>
> class Edge
> {
> ...
>[/color]

ok, I tried that, but when I try to access members of Node in Edge, I get an
error ;/

Heres the exact code

class Node;

class Edge

{

Node *N;

public:

Edge() { };

~Edge() { };

void func(int d)

{

N->a = d;

}

};



class Node

{

Edge *E;

public:

int a;

Node() { };

~Node() { };

};

void main(void) { Node T; }





The errors I get are

use of undefined type Node and left of ->a must point to a
class/struct/union.


Sep
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Classses of each other


That error is because you are trying to use an object of a type that
has not yet been fully defined.

If you are trying to use an object of a type, the type must be fully
known at the time of use. To fix this, seperate the definition of
function func from its declaration and place it after Node has been
defined.

class Node;

class Edge
{
....
void func(int d);
};

class Node
{
....
};

void Edge::func(int d)
{
N->a = d;
}

Jon Slaughter
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Classses of each other



"Sep" <Sep102@gmail.com> wrote in message
news:1110666478.894403.34370@g14g2000cwa.googlegro ups.com...[color=blue]
> That error is because you are trying to use an object of a type that
> has not yet been fully defined.
>
> If you are trying to use an object of a type, the type must be fully
> known at the time of use. To fix this, seperate the definition of
> function func from its declaration and place it after Node has been
> defined.
>
> class Node;
>
> class Edge
> {
> ...
> void func(int d);
> };
>
> class Node
> {
> ...
> };
>
> void Edge::func(int d)
> {
> N->a = d;
> }
>[/color]


Ok, I should have known that ;) Thanks! I guess I was expecting the compiler
to look a head when I referenced Node but I should have known better since I
had problems with that already.

Thanks again.

Jon


Closed Thread