Solved

Error: Incompatible types: regular procedure and method pointer

Posted on 2001-06-29
3
3,033 Views
Last Modified: 2012-06-21
I am getting an error (Incompatible types: regular procedure and method pointer) when using procedural types in the following maner:
TMyClass = class
  private
   function compare(a,b: integer): byte;
   procedure someProcedure;
end; {TMyClass}

procedure TMyClass.someProcedure;
begin
  mySort(compare);
end;

I have a unit called sort...

unit sort;
type
  compFunc = function (a,b: integer) : byte;

procedure mysort(compare: compFunc);
begin
  ...
end;

I can't figure out why I am getting this error message.
Thanks in advance
jeco
0
Comment
Question by:jeco
3 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6238318
Hi jeco,

it declared as function (a,b: Integer): byte  "of object".

you should declare
type
  compFunc = function (a,b: integer) : byte; of object;
and assign it not for standalone function, but for object method.

another way to pass address of function as parameter

type
  TMyClass = ...
  ...
   function compare(Proc: CompFunction);
  end;


function TMyClass.Compare(Proc: CompFunction);
begin
   if Proc(A, B) = 0 ...
....

-----
Igor.
0
 
LVL 3

Accepted Solution

by:
alzv earned 100 total points
ID: 6238325
You receive such message because you are trying to pass a method pointer as a parameter. But your procedure can only retrieve an entry point of regular function not a class method. If you want to pass a method into your procedure you need to modify CompFunc type declaration like this:

type
  CompFunc = function (a,b: integer) : byte of object;

Or if you want to pass both types of procedures (regular and methods) you need to overload your MySort procedure like this:

type
  CompFunc = function (a,b: integer) : byte;
  CompFuncMethod = function (a,b: integer) : byte of object;

procedure MySort(Compare: CompFunc); overload;
procedure MySort(Compare: CompFuncMethod); overload;

Best regards,
Alexey Zverev.
0
 

Author Comment

by:jeco
ID: 6239262
Thanks igor and Alexey for your answers. Both answers worked but since I ended up using overload procedures I will give the points to Alex
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now