By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,333 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,333 IT Pros & Developers. It's quick & easy.

Lists

P: n/a
Hi,
I like doing things myself because it helps me to learn languages. I'm in a
little trouble at the moment which someone might be able to help me.

I'm trying to create a list class. I have a struct which points to another
struct and so on:

private struct node

{

public object item;

public node next;

}
The problem is, it won't let me compile because it says:
Struct member 'List.node.next' of type 'List.node' causes a cycle in the
structure layout

Is there a way around this?

Thanks in advance,

Timothy.
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You said "I have a struct which points to another struct ...", but actually,
you've defined a struct which _contains_ another struct, and so on.
Obviously, that won't work, since you're going to end up with a data type of
infinite size.

The classic C approach to this problem would be to define "node *next"
instead, and dynamically create new nodes as you need them.

In C#, you would make node a reference type by declaring it "private class
node" instead of struct. If it helps, you can think of this as being
similar to the C++ "&node" Basically, this is internally represented as a
pointer, and every time you refer to a field, the pointer is implicitly
dereferenced.

HTH, Phil
"Timothy V" <tr****@msn.com> wrote in message
news:Of*************@tk2msftngp13.phx.gbl...
Hi,
I like doing things myself because it helps me to learn languages. I'm in a little trouble at the moment which someone might be able to help me.

I'm trying to create a list class. I have a struct which points to another
struct and so on:

private struct node

{

public object item;

public node next;

}
The problem is, it won't let me compile because it says:
Struct member 'List.node.next' of type 'List.node' causes a cycle in the
structure layout

Is there a way around this?

Thanks in advance,

Timothy.

Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.