www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Forward references

reply Jiyan <jiyan jiyan.info> writes:
Hi,
is there any document or text describing forward references?
It is kinda strange, i implemented a list structure which is 
kinda like this:

struct list(T)
{
private:

struct node
{
T val;
node* next;
node* prev;
}

node* head;
node* last;
size_t size;

  .....
}

The thing is when i implement following struct:

struct Tre
{
list!Tre a;
}

theoretically it should be constructable. But it gives me out a 
compiler error about Forward reference. Ok maybe the compiler at 
this point cant do that but ...

The strange thing is i somehow managed earlier without knowing to 
do exactly this in a much more complicated struct.
Can somebody enlighten me about this?
Feb 25 2018
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
works for me with 2.076.
Feb 25 2018
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/25/18 4:25 PM, Jiyan wrote:
 Hi,
 is there any document or text describing forward references?
 It is kinda strange, i implemented a list structure which is kinda like 
 this:
 
 struct list(T)
 {
 private:
 
 struct node
 {
 T val;
 node* next;
 node* prev;
 }
 
 node* head;
 node* last;
 size_t size;
 
   .....
 }
 
 The thing is when i implement following struct:
 
 struct Tre
 {
 list!Tre a;
 }
 
 theoretically it should be constructable. But it gives me out a compiler 
 error about Forward reference. Ok maybe the compiler at this point cant 
 do that but ...
 
 The strange thing is i somehow managed earlier without knowing to do 
 exactly this in a much more complicated struct.
 Can somebody enlighten me about this?
Looks like this was fixed in 2.064. What version of the compiler are you using? -Steve
Feb 25 2018
parent reply Jiyan <jiyan jiyan.info> writes:
On Sunday, 25 February 2018 at 22:20:13 UTC, Steven Schveighoffer 
wrote:
 On 2/25/18 4:25 PM, Jiyan wrote:
 [...]
Looks like this was fixed in 2.064. What version of the compiler are you using? -Steve
2.077.0 It is really strange, it works now but there still seems to be some strangeness about forward references. Thanks till now i guess :P
Feb 25 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/25/18 6:53 PM, Jiyan wrote:
 On Sunday, 25 February 2018 at 22:20:13 UTC, Steven Schveighoffer wrote:
 On 2/25/18 4:25 PM, Jiyan wrote:
 [...]
Looks like this was fixed in 2.064. What version of the compiler are you using? -Steve
2.077.0 It is really strange, it works now but there still seems to be some strangeness about forward references.
There are frequently issues with forward references. They are supposed to always work, but it's not always the case. -Steve
Feb 25 2018
prev sibling parent Elmar <chrehme gmx.de> writes:
Hello there,

I got a weird compilation error which was hard to debug (even for 
just a little program) and I thought, this is quite related to 
this thread. This is my error message:

```
***search.d(42,1): Error: class ***.XXX has forward references
***box.d(21,32): Error: template instance *** error instantiating
***.d(16,2):        instantiated from here: ...
...

```

It was odd, the fix was not obvious and the compiler didn't show 
me the place of the erroneous forward reference. The problem was 
a scoped `import` statement!! I had put the import statement for 
the Box class into the abstract base class Search, the only place 
where it is used, but this seems to cause a mess with many 
errors. The imported Box uses a sub class of Search as parameter 
type and since Search is extended by its subclasses it creates a 
circular reference in the subclass. The only way to fix was 
putting the import outside of the abstract base class (I assume 
because the import statement literally imports the Box together 
with the used subclass into Search itself).

The lesson is, you cannot put your import just everywhere.
Jun 10 2021