Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2024-01

Need some advice

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ppadilcdx

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"
program test;
{$mode objfpc}
uses
   fgl;
type
    pair = record
       x, y : integer;
    end;
operator < (a,b: pair) r:boolean;
begin
    if a.x < a.y then
       r := true
    else
       r := false;
end;
operator > (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;
begin
    Dict := TMyDict.Create;
    Dict.OnKeyCompare := @cmp;
    key.x := 2;
    key.y := 4;
    Dict.Add(key, 99);
    Value := Dict[key];
    writeln(Value);
    Dict.Free;
end.

Any advice appreciated. Thanks.

Pete

Reply by email

Luca Olivetti
to ppadilcdx

El 24/1/24 a les 20:31, ppadilcdx via fpc-pascal ha escrit:

Show quoted text 7 lines
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}
Show quoted text 7 lines
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

Luca

Reply by email

ppadilcdx
to Luca Olivetti

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.
Luca Olivetti wrote 41 lines
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

Reply by email

Sven Barth
to ppadilcdx

ppadilcdx via fpc-pascal schrieb am Mi.,
24. Jan. 2024, 22:07:

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 :

Your operator needs to establish a true order between the two passed in values. Right now your operators only check a, but not b. That is wrong and thus the map won't work correctly.

Regards,
Sven

Reply by email

ppadilcdx
to Sven Barth

Oh I didn't notice the typo, duh.  Thanks!!

Sven Barth wrote 19 lines
ppadilcdx via fpc-pascal schrieb am Mi., 24. Jan. 2024, 22:07: 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 : Your operator needs to establish a true order between the two passed in values. Right now your operators only check a, but not b. That is wrong and thus the map won't work correctly. Regards, Sven _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email