f



C to C++

Hi all

In C I have previously created structures that contain themselves e.g.

typdef struct Node_tag {
    struct Node_tag        ptrNodes;
    float                         nodeNumber
}    Node;

This worked fine, but trying to do the same in C++ doesn't work, any
suggestions.

Thanks

daveR


0
Charlotte
4/30/2004 9:39:02 AM
comp.games.programming.misc 1186 articles. 0 followers. jonathanfigaro (52) is leader. Post Follow

2 Replies
833 Views

Similar Articles

[PageSpeed] 29

This day of Fri, 30 Apr 2004 10:39:02 +0100, "Charlotte Dean"
<charlottedean@(NO5PAM)sheffield17a.fsnet.co.uk> proclaimed:

>Hi all
>
>In C I have previously created structures that contain themselves e.g.
>
>typdef struct Node_tag {
>    struct Node_tag        ptrNodes;
>    float                         nodeNumber
>}    Node;
>
>This worked fine, but trying to do the same in C++ doesn't work, any
>suggestions.
>
>Thanks
>
>daveR
>


For one you are missing a semi-colon after nodeNumber... for two, you spelled
'typedef' wrong.

Third, I would be *highly surprised* if you actually wrote a structure that
contained itself, and not a pointer to itself. If you think about it a structure
could not contain itself; that would create an infinite nesting. Structures
cannot contain themselves, only pointers to themselves.

Note that your name is "ptrNodes", which I'm assuming means "pointer to nodes" -
and you forgot the * in your structure.

And forth, in C++ you no longer need the typedef block. You can write:

struct Node {
   ...
};

and then use 'Node' as a type, e.g.

Node n;
n.nodeNumber = 123;
cout << n.nodeNumber;


-dhaley


-- 
~david-haley
0
David
4/30/2004 10:43:16 AM
Thanks for the answer.

1, spelling mistakes were not a prob as it wasn't cut n pasted just an
example.

2, I never thought about the never-ending nesting

All works fine now

Just need to implement A* in 48 hours now

thanks again

daveR
"David Haley" <junkmale221@hotmail.com> wrote in message
news:p8b490le7ohfq611jjbcco819c99ue8si6@4ax.com...
> This day of Fri, 30 Apr 2004 10:39:02 +0100, "Charlotte Dean"
> <charlottedean@(NO5PAM)sheffield17a.fsnet.co.uk> proclaimed:
>
> >Hi all
> >
> >In C I have previously created structures that contain themselves e.g.
> >
> >typdef struct Node_tag {
> >    struct Node_tag        ptrNodes;
> >    float                         nodeNumber
> >}    Node;
> >
> >This worked fine, but trying to do the same in C++ doesn't work, any
> >suggestions.
> >
> >Thanks
> >
> >daveR
> >
>
>
> For one you are missing a semi-colon after nodeNumber... for two, you
spelled
> 'typedef' wrong.
>
> Third, I would be *highly surprised* if you actually wrote a structure
that
> contained itself, and not a pointer to itself. If you think about it a
structure
> could not contain itself; that would create an infinite nesting.
Structures
> cannot contain themselves, only pointers to themselves.
>
> Note that your name is "ptrNodes", which I'm assuming means "pointer to
nodes" -
> and you forgot the * in your structure.
>
> And forth, in C++ you no longer need the typedef block. You can write:
>
> struct Node {
>    ...
> };
>
> and then use 'Node' as a type, e.g.
>
> Node n;
> n.nodeNumber = 123;
> cout << n.nodeNumber;
>
>
> -dhaley
>
>
> -- 
> ~david-haley


0
Charlotte
4/30/2004 11:07:45 AM
Reply: