Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Operator overloading not working

Status
Not open for further replies.

jagrroad

Programmer
Jul 8, 2008
4
US
I'm trying to get a basic operator overloading example working from here:

But the compiler complains about using the operator keyword in this line of the class definition:
class operator Add(a, b: OverloadsOps): OverloadsOps;

error E2123: PROCEDURE, FUNCTION, PROPERTY, or VAR expected (E2123) (IDE cursor is on the operator keyword)

I'm using Delphi explorer 2005 and the help file does include similar examples on operator overloading.

Do I have to do something to enable operator overloading??

type
OverloadsOps = class
private
FField: Integer;
public
class operator Add(a, b: OverloadsOps): OverloadsOps;
class operator Subtract(a, b: OverloadsOps): OverloadsOps;
end;

class operator OverloadsOps.Add(a, b: OverloadsOps): OverloadsOps;
begin
Result := OverloadsOps.Create;
Result.FField := a.FField + b.FField;
end;

class operator OverloadsOps.Subtract(a, b: OverloadsOps): OverloadsOps;
begin
Result := OverloadsOps.Create;
Result.FField := a.FField - b.FField;
end;
 
Are you on Delphi .NET, or are you on a Win32 target? I can't say if I know if this is valid Delphi .NET target code, but I know it's not valid Delphi Win32 code by a long shot.

See this and use the example from it instead and you will likely get farther:

Overload operator

----------
Measurement is not management.
 
Yes, it's for .NET only. I ran it through TD2006 (Win32) and came back with 27 errors as similar as yours.

You're on a Win32 target trying to implement a .NET feature.
So either do this in .NET or do the procedure/function overload like I linked to.

----------
Measurement is not management.
 
I'm using Delphi Win32. The documentation states:

Delphi for .NET and Delphi for Win32 allow certain functions, or "operators" to be overloaded within record declarations. Delphi for .NET also allows overloading within class declarations.

So regarding Win32 Delphi, is any operator overloading allowed at all (even outside of class declarations)? If so, can I see an example.
 
I'm using Delphi Win32. The documentation states:

You're learning now that documentation doesn't mean anything unless it can be proven true. Documentation in later versions of Delphi is especially atrocious compared to the earlier versions (I like my D3 documentation about 2000% better). Your experience is confirming it false. And I'm confirming it for you by compiling the same code in TD2006 (essentially Delphi Explorer 2006).

So regarding Win32 Delphi, is any operator overloading allowed at all (even outside of class declarations)?

As I stated, it's a .NET only feature, it got proven by both you and me, and I'll even let Joanna Carter, TeamB member (basically Borland's equivalent to the MS MVP), tell you.

----------
Measurement is not management.
 
Oops, my fault, the documentation is correct here. On Win32, the code works when I change "class" to "record":

OverloadOps = record

So operator overloading only supported for records in Win32 until the next version.
 
Hi jagrroad,

I've encountered the same problem some time ago. I hope commodore will bring us that power.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top