[fpc-pascal] Need some advice

ppadilcdx ppadilcdx at gmail.com
Wed Jan 24 22:07:22 CET 2024


Thanks for that, now it compiles but it doesn't work, i.e. I added a 
pair and value and then used trygetdata and it can't find it. So perhaps 
I'm missing something else :

program test;
{$mode objfpc}
{$modeswitch advancedrecords}
uses
   fgl;

type
    pair = record
       x, y : integer;
       class operator > (a,b: pair) r:boolean;
       class operator < (a,b: pair) r:boolean;
    end;

class operator pair.< (a,b: pair) r:boolean;
begin
    if a.x < a.y then
       r := true
    else
       r := false;
end;

class operator pair.> (a,b: pair) r:boolean;
begin
    if a.x > a.y then
       r := true
    else
       r := false;
end;

function cmp(const a,b : pair) : LongInt;
begin
    if a < b then
       cmp := -1
    else
       if a > b then
          cmp := 1
       else
          cmp := 0;
end;

type
   TMyDict = specialize TFPGMap<pair, integer>;
var
   Dict: TMyDict;
   key: pair;
   Value: Integer;
   flg: boolean;
begin
    Dict := TMyDict.Create;
    Dict.OnKeyCompare := @cmp;
    key.x := 2;
    key.y := 4;
    Dict.Add(key, 99);
    flg := Dict.trygetdata(key, Value);
    writeln(flg);
    writeln(Value);
    Dict.Free;
end.

On 1/24/24 12:34PM, Luca Olivetti via fpc-pascal wrote:
> El 24/1/24 a les 20:31, ppadilcdx via fpc-pascal ha escrit:
>> Trying to use the fgl unit, specifically the TFPGMap.   Below is a 
>> simple program I'm trying to compile; it gives me two errors:
>>
>> fgl.pp(1582,18) Error: Operator is not overloaded: "pair" < "pair"
>> fgl.pp(1584,23) Error: Operator is not overloaded: "pair" > "pair"
>>
>>
>
> add a {$Modeswitch advancedrecords} and use class operators
>
>> program test;
>> {$mode objfpc}
>   {$modeswitch advancedrecords}
>>
>> uses
>>    fgl;
>>
>> type
>>     pair = record
>>        x, y : integer;
>         class operator < (a,b:pair):boolean;
>         class operator > (a,b:pair):boolean;
>>     end;
>>
>
>
>> operator < (a,b: pair) r:boolean;
>
>    class operator pair.<(a,b:pair):boolean;
>
>> begin
>>     if a.x < a.y then
>>        r := true
>          result:=true
>>     else
>>        r := false;
>          result:=false;
>> end;
>
> bye


More information about the fpc-pascal mailing list