digitalmars.D.learn - Binary Heap Errors Class level vs function level
- Chris Pons (60/60) Apr 04 2012 I'm still messing around with binary heaps. I've successfully
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (33/89) Apr 04 2012 To get the type, use an alias. Also, only compile-time expressions can
- Chris Pons (2/124) Apr 04 2012
I'm still messing around with binary heaps. I've successfully created and used it on the function level but for some reason when I move it to the class level I get an error. Furthermore, i'm not entirely sure how to use a binary heap without auto as the type. class AStar { ReferenceNode[] openListContainer; auto openList = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); // Error } Error 1 Error: template instance BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") does not match template declaration BinaryHeap(Store,alias less = "a < b") if (isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[]))) C:\Users\CP\Documents\Visual Studio 2010\Projects\D\STDS\NPC.d 101 However this is ok: class AStar { void RandomFunction() { ReferenceNode[] openListContainer; openListContainer.length = 500; auto openList = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); //This is ok } } I'd also like to try this, but can't seem to figure it out: class AStar { ReferenceNode[] openListContainer; auto openList; //no identifier for declarator openlist this() { openListContainer.length = 500; openList = = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); } } If I know what type openList was, after creating the heap, I could simply use that. I tried using typeid( openList ) to find out. I got the type being something like: BinaryHeap!(Referencenode[], "a.fScore > b.fScore") openList; However trying to use this as the type gives me this error: Error 1 Error: template instance BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") does not match template declaration BinaryHeap(Store,alias less = "a < b") if (isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[]))) C:\Users\CP\Documents\Visual Studio 2010\Projects\D\STDS\NPC.d 101 Error 2 Error: BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") is used as a type C:\Users\CP\Documents\Visual Studio 2010\Projects\D\STDS\NPC.d 101 I'm just trying to get a variable called openList to the class level so it doesn't keep getting initialized everytime the function is called. Any ideas on how to work this out?
Apr 04 2012
On 04/04/2012 05:24 PM, Chris Pons wrote:I'm still messing around with binary heaps. I've successfully created and used it on the function level but for some reason when I move it to the class level I get an error. Furthermore, i'm not entirely sure how to use a binary heap without auto as the type. class AStar { ReferenceNode[] openListContainer; auto openList = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); // Error }To get the type, use an alias. Also, only compile-time expressions can be used to initialize members with in-class initializers. So you must initialize openListContainer in a constructor: import std.container; struct ReferenceNode { int fScore() property { return 0; } } class AStar { ReferenceNode[] openListContainer; alias BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore") MyBinaryHeap; MyBinaryHeap openList; this() { openList = MyBinaryHeap(openListContainer, 0 ); } } void main() { auto as = new AStar(); }Error 1 Error: template instance BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") does not match template declaration BinaryHeap(Store,alias less = "a < b") if (isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[]))) C:\Users\CP\Documents\Visual Studio 2010\Projects\D\STDS\NPC.d 101 However this is ok: class AStar { void RandomFunction() { ReferenceNode[] openListContainer; openListContainer.length = 500; auto openList = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); //This is ok } }That is ok, because now the initial value need not be a compile-time expression. RandomFunction() gets executed and the right-hand side is evaluatod. Fine.I'd also like to try this, but can't seem to figure it out: class AStar { ReferenceNode[] openListContainer; auto openList; //no identifier for declarator openlistThat cannot work. Being a statically-typed language, D must know the exact type of the variables at definition time.this() { openListContainer.length = 500; openList = = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); } } If I know what type openList was, after creating the heap, I could simply use that. I tried using typeid( openList ) to find out. I got the type being something like: BinaryHeap!(Referencenode[], "a.fScore > b.fScore") openList; However trying to use this as the type gives me this error: Error 1 Error: template instance BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") does not match template declaration BinaryHeap(Store,alias less = "a < b") if (isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[]))) C:\Users\CP\Documents\Visual Studio 2010\Projects\D\STDS\NPC.d 101 Error 2 Error: BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore") is used as a type C:\Users\CP\Documents\Visual Studio 2010\Projects\D\STDS\NPC.d 101 I'm just trying to get a variable called openList to the class level so it doesn't keep getting initialized everytime the function is called. Any ideas on how to work this out?I hope the program above is clear. :) Ali
Apr 04 2012
Thank you! That helps out quite a bit. On Thursday, 5 April 2012 at 01:28:12 UTC, Ali Çehreli wrote:On 04/04/2012 05:24 PM, Chris Pons wrote:I'm still messing around with binary heaps. I've successfullycreatedand used it on the function level but for some reason when Imove it tothe class level I get an error. Furthermore, i'm not entirelysure howto use a binary heap without auto as the type. class AStar { ReferenceNode[] openListContainer; auto openList = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); // Error }To get the type, use an alias. Also, only compile-time expressions can be used to initialize members with in-class initializers. So you must initialize openListContainer in a constructor: import std.container; struct ReferenceNode { int fScore() property { return 0; } } class AStar { ReferenceNode[] openListContainer; alias BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore") MyBinaryHeap; MyBinaryHeap openList; this() { openList = MyBinaryHeap(openListContainer, 0 ); } } void main() { auto as = new AStar(); }Error 1 Error: template instanceBinaryHeap!(ReferenceNode[],"a.fScore >b.fScore") BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore")does notmatch template declaration BinaryHeap(Store,alias less = "a <b") if(isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[])))C:\Users\CP\Documents\VisualStudio 2010\Projects\D\STDS\NPC.d 101 However this is ok: class AStar { void RandomFunction() { ReferenceNode[] openListContainer; openListContainer.length = 500; auto openList = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); //This is ok } }That is ok, because now the initial value need not be a compile-time expression. RandomFunction() gets executed and the right-hand side is evaluatod. Fine.I'd also like to try this, but can't seem to figure it out: class AStar { ReferenceNode[] openListContainer; auto openList; //no identifier for declarator openlistThat cannot work. Being a statically-typed language, D must know the exact type of the variables at definition time.this() { openListContainer.length = 500; openList = = BinaryHeap!(ReferenceNode[], "a.fScore > b.fScore")(openListContainer, 0 ); } } If I know what type openList was, after creating the heap, Icouldsimply use that. I tried using typeid( openList ) to findout. I got thetype being something like: BinaryHeap!(Referencenode[], "a.fScore > b.fScore") openList; However trying to use this as the type gives me this error: Error 1 Error: template instanceBinaryHeap!(ReferenceNode[],"a.fScore >b.fScore") BinaryHeap!(ReferenceNode[],"a.fScore > b.fScore")does notmatch template declaration BinaryHeap(Store,alias less = "a <b") if(isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[])))C:\Users\CP\Documents\VisualStudio 2010\Projects\D\STDS\NPC.d 101 Error 2 Error: BinaryHeap!(ReferenceNode[],"a.fScore >b.fScore") isused as a type C:\Users\CP\Documents\Visual Studio 2010\Projects\D\STDS\NPC.d 101 I'm just trying to get a variable called openList to theclass level soit doesn't keep getting initialized everytime the function iscalled.Any ideas on how to work this out?I hope the program above is clear. :) Ali
Apr 04 2012