www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Forward Reference

reply "Anibal" <anibal.palma toc.cl> writes:
Hi everyone,

I'm trying to something like a tree structure.

The following:

import std.container;
class Tree
{
         private SList!Tree subTree;
}

Produces: class Tree no size yet for forward reference.

How i should proceed in order to keep this declaration?

Thanks a lot!

PD: (You guys are incredibly quick to answer, that's awesome!)
Oct 09 2014
next sibling parent reply "Njkp" <Njkp nowhere.lm> writes:
On Thursday, 9 October 2014 at 19:04:56 UTC, Anibal wrote:
 Hi everyone,

 I'm trying to something like a tree structure.

 The following:

 import std.container;
 class Tree
 {
         private SList!Tree subTree;
 }

 Produces: class Tree no size yet for forward reference.

 How i should proceed in order to keep this declaration?

 Thanks a lot!

 PD: (You guys are incredibly quick to answer, that's awesome!)
make a pointer list instead, which has a fixed size: --- import std.container; class Tree { private SList!(Tree*) subTree; } ---
Oct 09 2014
parent "Nimrod the Shlomo" <Nim fghri.ira> writes:
On Thursday, 9 October 2014 at 19:26:20 UTC, Njkp wrote:
 On Thursday, 9 October 2014 at 19:04:56 UTC, Anibal wrote:
 Hi everyone,

 I'm trying to something like a tree structure.

 The following:

 import std.container;
 class Tree
 {
        private SList!Tree subTree;
 }

 Produces: class Tree no size yet for forward reference.

 How i should proceed in order to keep this declaration?

 Thanks a lot!

 PD: (You guys are incredibly quick to answer, that's awesome!)
make a pointer list instead, which has a fixed size: --- import std.container; class Tree { private SList!(Tree*) subTree; } ---
fixed size: the item as pointer. But, BTW if you put a class as an item collection it's always a fixed size, a class is always a ptr. Better, you 'll be able to use the pointer without dereference. But you prefear the solution of the other guy...It's ok. I just hope that your subtree will not have to be reorganized too often...because with an array, it'll be very very very slow...
Oct 10 2014
prev sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 09 Oct 2014 19:04:55 +0000
Anibal via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 Hi everyone,
=20
 I'm trying to something like a tree structure.
=20
 The following:
=20
 import std.container;
 class Tree
 {
          private SList!Tree subTree;
 }
=20
 Produces: class Tree no size yet for forward reference.
=20
 How i should proceed in order to keep this declaration?
do you really need single-linked list for that? D has dynamic arrays, which can be used instead. class Tree { private Tree[] subTree; } you can append items to dynamic array with "~=3D", get it length with .length and so on. seems that you trying to copy some C code (or writing in C manner), amirite? it is possible to use D as "better C", but D has alot more to offer. did you seen this excellent book: http://ddili.org/ders/d.en/ ? it will teach you some nice things which are absent in C. read it even if you are seasoned C programmer. you'll see a joy of dynamic arrays, slices, ranges and templates, nicely explained.
Oct 09 2014
parent "Anibal" <anibal.palma toc.cl> writes:
On Thursday, 9 October 2014 at 19:29:13 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Thu, 09 Oct 2014 19:04:55 +0000
 Anibal via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 Hi everyone,
 
 I'm trying to something like a tree structure.
 
 The following:
 
 import std.container;
 class Tree
 {
          private SList!Tree subTree;
 }
 
 Produces: class Tree no size yet for forward reference.
 
 How i should proceed in order to keep this declaration?
do you really need single-linked list for that? D has dynamic arrays, which can be used instead. class Tree { private Tree[] subTree; } you can append items to dynamic array with "~=", get it length with .length and so on. seems that you trying to copy some C code (or writing in C manner), amirite? it is possible to use D as "better C", but D has alot more to offer. did you seen this excellent book: http://ddili.org/ders/d.en/ ? it will teach you some nice things which are absent in C. read it even if you are seasoned C programmer. you'll see a joy of dynamic arrays, slices, ranges and templates, nicely explained.
Thanks a lot, declaring it as an array solved mi troubles!
Oct 09 2014