Thread overview
12 line program... `main` is a nested function when trying to use redblacktree. Beginner error???
June 27

import std.container : RedBlackTree;

int main()
{

struct Location {
    int x;
    int y;
}

struct Node{
    this(Location loc, uint f) {
        this.loc = loc;
        this.f = f;
    }
    Location loc;
    uint f;
}

auto priorityQueue = new RedBlackTree!(Node, "a.f < b.f", true); // true: allowDuplicates

// C:\D\dmd2\windows\bin64....\src\phobos\std\container\rbtree.d(806): Error: main is
// a nested function and cannot be accessed from
// std.container.rbtree.RedBlackTree!(Node, "a.f < b.f", true).RedBlackTree.allocate

return 0;
}

June 27
On Thu, Jun 27, 2024 at 08:56:13PM +0000, WhatMeWorry` via Digitalmars-d-learn wrote:
> import std.container : RedBlackTree;
> 
> int main()
> {
> 
>     struct Location {
>         int x;
>         int y;
>     }
> 
>     struct Node{
>         this(Location loc, uint f) {
>             this.loc = loc;
>             this.f = f;
>         }
>         Location loc;
>         uint f;
>     }
> 
> auto priorityQueue = new RedBlackTree!(Node, "a.f < b.f", true); // true:
> allowDuplicates
> 
> // C:\D\dmd2\windows\bin64\..\..\src\phobos\std\container\rbtree.d(806):
> Error: `main` is
> // a nested function and cannot be accessed from
> // `std.container.rbtree.RedBlackTree!(Node, "a.f < b.f",
> true).RedBlackTree.allocate`
> 
> return 0;
> }

Move the declaration of Location and Node outside of main and it should work.


T

-- 
Almost all proofs have bugs, but almost all theorems are true. -- Paul Pedersen
June 27
Thanks, that did the trick. Not sure why having the declarations at global scope (or is it module scope in D) would work versus having them at local scope?
June 27
On Thu, Jun 27, 2024 at 10:14:06PM +0000, WhatMeWorry via Digitalmars-d-learn wrote:
> Thanks, that did the trick. Not sure why having the declarations at global scope (or is it module scope in D) would work versus having them at local scope?

If you stuck 'static' to the local scope declarations that ought to fix it too.  The main problem is that without 'static', there will be a hidden context pointer in the struct that points to the main's local scope. This can't work with RBTree because it cannot obtain a pointer to this local scope when it needs to create new nodes.


T

-- 
Freedom: (n.) Man's self-given right to be enslaved by his own depravity.