Connecting Tech Pros Worldwide Help | Site Map

Classses of each other

  #1  
Old July 23rd, 2005, 02:58 AM
Jon Slaughter
Guest
 
Posts: n/a
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, 02:58 AM
Sep
Guest
 
Posts: n/a

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, 02:58 AM
Jon Slaughter
Guest
 
Posts: n/a

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, 02:58 AM
Sep
Guest
 
Posts: n/a

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, 02:58 AM
Jon Slaughter
Guest
 
Posts: n/a

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, 02:58 AM
Sep
Guest
 
Posts: n/a

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, 02:59 AM
Jon Slaughter
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
ClickOnce program fails to run properly after update Michael D. Reed answers 2 November 21st, 2006 09:05 AM
ClickOnce program fails to run properly after update Michael D. Reed answers 2 November 21st, 2006 09:05 AM
How to share a set of classes from two IIS web sites? Quentin Huo answers 4 November 18th, 2005 11:10 PM
OOP: constructors of base class in new() Ramprasad A Padmanabhan answers 4 July 17th, 2005 05:30 AM