Sign in Join

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

Mailing lists / fpc-pascal / 2010-09

"Error: Impossible operator overload" ...why?

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Graeme Geldenhuys

Hi,

I'm using FPC 2.4.3

Why is the following operator overload not allowed? Sometimes TfpgColor can be a RRGGBB value (eg: clRed), and sometimes it is an index to a color palette (eg: clWindowBackground). So I want to do comparisons between two such colors.

The compiler error I get:

..../programming/fpgui/src/corelib/fpg_main.pas(414,58) Error:
Impossible operator overload

The types are defined as follows:

type
  TfpgColor = type longword;
  TFPColor = record
    Red: word;
    Green: word;
    Blue: word;
    Alpha: word;
  end;

... now the unit with the compiler error ...

interface section:

  operator = (const AColor1, AColor2: TfpgColor) b: Boolean;

implementation section:

operator = (const AColor1, AColor2: TfpgColor) b: Boolean;
var
  c1, c2: TFPColor;
begin
  c1  := fpgColorToFPColor(AColor1);
  c2  := fpgColorToFPColor(AColor2);
  b   := (c1.Red = c2.Red) and (c1.Green = c2.Green) and
         (c1.Blue = c2.Blue) and (c1.Alpha = c2.Alpha);
end;
Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Jonas Maebe
to Graeme Geldenhuys
Graeme Geldenhuys wrote
type TfpgColor = type longword;

[snip]

operator = (const AColor1, AColor2: TfpgColor) b: Boolean;

You cannot overload (or rather change the meaning of) operators for operator/type combinations that are natively handled by the compiler. "type longword" is handled the same as a regular longword by the compiler as far as all operators are concerned.

Jonas

Reply by email

Graeme Geldenhuys
to Jonas Maebe
Jonas Maebe wrote 5 lines
You cannot overload (or rather change the meaning of) operators for operator/type combinations that are natively handled by the compiler. "type longword" is handled the same as a regular longword by the compiler as far as all operators are concerned.

OK, I thought the TfpgColor type will act as a new type, and operator overloading could be possible. I guess I was wrong. :-/

Is there any way to tell the compiler to treat TfpgColor as a new type?

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email