RedBlackTree and myClass

Tobi G. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 3 08:25:31 PST 2016


On Sunday, 3 January 2016 at 14:49:59 UTC, tsbockman wrote:
> On Sunday, 3 January 2016 at 10:55:05 UTC, AntonSotov wrote:
>> import std.container.rbtree;
>>
>> class myClass {
>>     string str;
>> }
>>
>>
>> int main()
>> {
>>     auto tree = new RedBlackTree!myClass;
>>     return 0;
>> }
>>
>>
>> Error: mutable method object.Object.opCmp is not callable 
>> using a inout object
>> Error: template instance std.functional.binaryFun!("a < b", 
>> "a", "b").binaryFun!(inout(myClass), myClass) error 
>> instantiating
>> /////////////////////////////////////////
>>
>> How to use RedBlackTree of the objects of of its class?
>
> Anyway, it's not too hard if you understand what's going on, 
> and all of the functions I added are good things to have 
> anyway, because lots of generic code expects some or all of 
> them. But, the error messages aren't all that helpful if you 
> didn't already know most of that.

But as a beginner it is quite disappointing to write all of these 
functions to just get it work.
Maybe i am a bad programmer, but i don't write and use the member 
functions above that often.
I often use the 'less' in the template arguments to get such 
things as comparison done, and implement these functions only if 
i have to..
To get it work this should be enough:

import std.container.rbtree;

class myClass {
     string str;
	
     override string toString() const {
         return "{myClass: " ~ str ~ "}"; }
}

void main()
{
     auto tree = new RedBlackTree!(myClass, "a.str < b.str");
}

~ togrue




More information about the Digitalmars-d-learn mailing list