Connecting Tech Pros Worldwide Help | Site Map

Classses of each other

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 01:58 AM
Jon Slaughter
Guest
 
Posts: n/a
Default Classses of each other

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



  #2  
Old July 23rd, 2005, 01:58 AM
Sep
Guest
 
Posts: n/a
Default Re: Classses of each other

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

class Node;

class Edge
{
....

  #3  
Old July 23rd, 2005, 01:58 AM
Jon Slaughter
Guest
 
Posts: n/a
Default 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.


  #4  
Old July 23rd, 2005, 01:58 AM
Sep
Guest
 
Posts: n/a
Default 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.

  #5  
Old July 23rd, 2005, 01:58 AM
Jon Slaughter
Guest
 
Posts: n/a
Default 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.


  #6  
Old July 23rd, 2005, 01:58 AM
Sep
Guest
 
Posts: n/a
Default 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;
}

  #7  
Old July 23rd, 2005, 01:59 AM
Jon Slaughter
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.