Lazarus

Free Pascal => General => Topic started by: soerensen3 on January 03, 2018, 01:19:47 am

Title: operator overloading and TFPGList with objects
Post by: soerensen3 on January 03, 2018, 01:19:47 am
I'm having issues using objects or records together with fpc List.
When I try to specialize a list it gives me an error (without source position) that the = operator for the record/object is not overloaded. So far, so good, even if I cannot find the line where it uses the = operator for my type in the body of TFPGList.

But even if I overload the operator it is still not working:
To compile just add the unit to the uses section of an empty program.
Code: Pascal  [Select][+][-]
  1. Unit Unit1;
  2. interface
  3.  
  4. uses
  5.   fgl;
  6.  
  7. type
  8.   { TVec2 }
  9.  
  10.   TVec2 = object
  11.   end;
  12.  
  13. operator = ( A, B: TVec2 ): Boolean; //declare operator before specialization
  14.  
  15. type //start a new type section after operator declaration
  16.   TVec2List = specialize TFPGList < TVec2 >;
  17.  
  18.  
  19. implementation
  20.  
  21. operator = ( A, B: TVec2 ): Boolean;
  22. begin
  23.   Result:= True;
  24. end;
  25.  
  26. end.
  27.  

It can be done with records with a modeswitch and a class operator. For objects it does not work though:
http://forum.lazarus.freepascal.org/index.php/topic,36469.msg243242.html#msg243242

Is it possible to use TFGPList with objects? (I need inheritance)
Title: Re: operator overloading and TFPGList with objects
Post by: Blaazen on January 03, 2018, 01:42:31 am
See https://forum.lazarus.freepascal.org/index.php?topic=38887.0 (https://forum.lazarus.freepascal.org/index.php?topic=38887.0)

Note that you will have to switch from object to record. Also, I don't know if the directive {$MODESWITCH ADVANCEDRECORDS} is available in 3.0.2. Maybe you should upgrade to 3.0.4 or use SVN.
Title: Re: operator overloading and TFPGList with objects
Post by: soerensen3 on January 03, 2018, 01:26:44 pm
Thank you Blaazen!

I knew of the modeswitch (see the link in my post) but my question was if it was possible with objects as well, because I wanted inheritance.
But now I thought I might as well be using record helpers.

Oh and I use 3.0.4 already. The problem with the signature is you always forget updating it :)
TinyPortal © 2005-2018