Recent

Author Topic: Overloading operator with LGenerics not working  (Read 1114 times)

Frogfather

  • Jr. Member
  • **
  • Posts: 55
Overloading operator with LGenerics not working
« on: February 01, 2024, 01:59:02 pm »
Hello,

I'm trying out the TGComparableBinHeapMin class from the LGenerics package.

This requires that the type used in the queue has defined the comparison operator <

So I have

Code: Pascal  [Select][+][-]
  1. type
  2.    TMyType = record
  3.     someProperty:integer;
  4.     end;
  5.  
  6. operator < (a, b: TMyType): Boolean;  
  7.  

and then in the implementation

Code: Pascal  [Select][+][-]
  1. var
  2.   pq__: specialize TGComparableBinHeapMin<TMyType>;  
  3.  
  4. operator < (a, b: TMyType): Boolean;
  5. begin
  6.   result:= a.someProperty < b.someProperty;
  7. end;  
  8.  
 

Unfortunately the compiler complains that
lgpriorityqueue.pas(1004,15) Error: Operator is not overloaded: "TMyType" < "TMyType"

I've. not tried operator overloading before so it's entirely possible I'm doing something very obvious but if anyone could point me in the correct direction it would be appreciated!

Thanks

John C

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Overloading operator with LGenerics not working
« Reply #1 on: February 01, 2024, 02:02:29 pm »
Make it an record operator instead of a global one. see manual
Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: Overloading operator with LGenerics not working
« Reply #2 on: February 01, 2024, 02:27:38 pm »
The lgenerics part is not relevant given your code. And that code simply works:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. type
  3.    TMyType = record
  4.     someProperty:integer;
  5.     end;
  6.  
  7. operator < (const a, b: TMyType): Boolean;
  8. begin
  9.   result:= a.someProperty < b.someProperty;
  10. end;  
  11. begin
  12. end.
  13.  
So what is your real question?
« Last Edit: February 01, 2024, 02:29:25 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

bytebites

  • Hero Member
  • *****
  • Posts: 778

Frogfather

  • Jr. Member
  • **
  • Posts: 55
Re: Overloading operator with LGenerics not working
« Reply #4 on: February 01, 2024, 04:43:40 pm »
TRon

Many thanks that works!

Best wishes

John C

 

TinyPortal © 2005-2018