Solved

Error: Incompatible types: regular procedure and method pointer

Posted on 2001-06-29
3
3,308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 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