Recent

Author Topic: [SOLVED] Overloading equity operator doesn't compil...  (Read 5308 times)

gelinp

  • Full Member
  • ***
  • Posts: 114
[SOLVED] Overloading equity operator doesn't compil...
« on: November 30, 2015, 04:15:14 pm »
Hello,

I'm overloading an equity operator like this (It's my memory, I'm not with my computer) :

Code: Pascal  [Select][+][-]
  1. operator = (i1, i2 : TindiceDec) : boolean
  2. begin
  3.   result := ...
  4. end;

And there is an error message telling there is compatibilty problem with either type of parameter, type of result... I don't understand what is wrong with this syntaxe. Could you explain me ?

Lazarus version : 1.4.4
« Last Edit: December 31, 2015, 08:01:22 am by gelinp »
My configuration is : Lazarus 1.6+dfsg-1 / FPC 3.0.0 / Debian Mint / x86_64-linux-gtk2

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

gelinp

  • Full Member
  • ***
  • Posts: 114
Re: Overloading equity operator doesn't compil...
« Reply #2 on: November 30, 2015, 07:56:07 pm »
Yes, I know this page. What can I understand ? Nothing I will see to help me. I tried the formdeclaration :

Code: Pascal  [Select][+][-]
  1. operator = (i1, i2 : TindiceDec) b : boolean;

But it's the same problem. This is just an other way to declare the operator...

So the complet code of my operator is :

Code: Pascal  [Select][+][-]
  1. operator =  (i1, i2 : TindiceDec) : boolean;
  2. begin
  3.      result := AnsiCompareText(i1.indice, i2.indice)=0;
  4. end;    

with indice a property returning a string. And the exact error message is :

cotation.pas(201,44) Error: Impossible operator overload.
The combination of operator, arguments and return type are incompatible.


I can't find what is the compatibility problem... So could anybody show me what is the problem ?
« Last Edit: November 30, 2015, 08:01:03 pm by gelinp »
My configuration is : Lazarus 1.6+dfsg-1 / FPC 3.0.0 / Debian Mint / x86_64-linux-gtk2

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Overloading equity operator doesn't compil...
« Reply #3 on: November 30, 2015, 08:11:10 pm »
You cannot overload operator = this way because TIndiceDec is class and operator = is already used for comparing classes (i.e. it compares pointers to instances).
If you redeclare TIndiceDec to record, it will work.
« Last Edit: November 30, 2015, 08:22:40 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

gelinp

  • Full Member
  • ***
  • Posts: 114
Re: Overloading equity operator doesn't compil...
« Reply #4 on: November 30, 2015, 09:27:37 pm »
But I can't redeclare TindiceRec to a record, because it's a class with a lot of methods, and I don't want to manage a record but a class... So, I understand I could try an other issue : declare a record RIndiceRec and writing a cast method to the class in order to use class in place of record... Is it possible to write a cast operator for a class ?
My configuration is : Lazarus 1.6+dfsg-1 / FPC 3.0.0 / Debian Mint / x86_64-linux-gtk2

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Overloading equity operator doesn't compil...
« Reply #5 on: November 30, 2015, 10:03:36 pm »
Your chance is advanced record (record with methods). You can activate it this way:
Code: Pascal  [Select][+][-]
  1. {$modeswitch advancedrecords}

Or better, instead of overloaded operator =, use class method:
Code: Pascal  [Select][+][-]
  1. class function TIndiceDec.Compare(i1, i2: TIndiceDec): Boolean;
  2. begin
  3.   Result:=AnsiCompareText(i1.indice, i2.indice)=0;
  4. end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

gelinp

  • Full Member
  • ***
  • Posts: 114
Re: Overloading equity operator doesn't compil...
« Reply #6 on: November 30, 2015, 10:59:01 pm »
Ok, thank you very much ! 

But do this method compare permit to use the = operator or do I need to call compar method each time a comapraison occure ?
My configuration is : Lazarus 1.6+dfsg-1 / FPC 3.0.0 / Debian Mint / x86_64-linux-gtk2

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Overloading equity operator doesn't compil...
« Reply #7 on: November 30, 2015, 11:12:41 pm »
You need to call the method. Since it is the class method, you can call it
Code: Pascal  [Select][+][-]
  1. b:=TIndiceDec.Compare(i1, i2);
  2. or
  3. b:=i1.Compare(i1, i2);
  4. or
  5. b:=i2.Compare(i1, i2);
  6.  
with the same result.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

gelinp

  • Full Member
  • ***
  • Posts: 114
Re: Overloading equity operator doesn't compil...
« Reply #8 on: December 02, 2015, 06:44:56 am »
I'm disappointed by this aspect of free pascal bacause I would like to use class heritage and I'm wondering about the use of collections class because usually they use operators to classify objects... So what about templates and collections fonctionalities if I can't overload operators with calss ?
My configuration is : Lazarus 1.6+dfsg-1 / FPC 3.0.0 / Debian Mint / x86_64-linux-gtk2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Overloading equity operator doesn't compil...
« Reply #9 on: December 02, 2015, 07:35:15 am »
Yes, I know this page. What can I understand ? Nothing I will see to help me.
"The comparison operator can be overloaded to compare two different types or to compare two equal types that are not basic types", including classes which carry pointer semantics. The reasoning is quite well explained here.

gelinp

  • Full Member
  • ***
  • Posts: 114
Re: Overloading equity operator doesn't compil...
« Reply #10 on: December 02, 2015, 08:22:32 am »
Some answers says operators could be overload only with records, others says it could be even for classes... Explanations are a problem of memory leak with class references. But C++, Java provides there solutions (garbage collector...) to autorize operator overloading... My need is to overload a class not a record, and to use collections and templates. I can't understand how to use collection or template witout operator overloading for classes...
My configuration is : Lazarus 1.6+dfsg-1 / FPC 3.0.0 / Debian Mint / x86_64-linux-gtk2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Overloading equity operator doesn't compil...
« Reply #11 on: December 02, 2015, 09:45:20 am »
But C++, Java provides there solutions (garbage collector...) to autorize operator overloading...
C++ classes are analogous to Pascal's objects, not classes, which essentially are records. Java does not have operator overloading at all, by design (which its author violates by overloading + for strings). So your bet is to use objects instead of classes, hopefully that would work.

 

TinyPortal © 2005-2018