Recent

Author Topic: [SOLVED]Beginthread incompatible type error  (Read 513 times)

cov

  • Full Member
  • ***
  • Posts: 231
[SOLVED]Beginthread incompatible type error
« on: November 20, 2020, 09:01:25 am »
Using the example from the tutorial, this is my code:
Code: Pascal  [Select][+][-]
  1. function TForm1.getPedalInput: ptrint;
  2. var
  3.   pedalInput: TFileStream;
  4. begin
  5.   pedalInput:=TFileStream.Create('/dev/usb/hiddev0',fmOpenRead);
  6.   while True do begin
  7.     if pedalInput.Size>0 then
  8.     Memo1.Lines.Add('pressed');
  9.   end;
  10. end;    

I'm calling it thus:
Code: Pascal  [Select][+][-]
  1. BeginThread(@getPedalInput);
This is the error:
Quote
unit1.pas(110,29) Error: Incompatible type for arg no. 1: Got "<procedure variable type of function(Pointer):Int64 of object;Register>", expected "<procedure variable type of function(Pointer):Int64;Register>"

Lazarus 2.0.10 rUnversioned directory FPC 3.2.0 x86_64-linux-gtk2

~ Dave
« Last Edit: November 22, 2020, 08:00:58 am by cov »

Zvoni

  • Sr. Member
  • ****
  • Posts: 423
Re: Beginthread incompatible type error
« Reply #1 on: November 20, 2020, 09:23:32 am »
Quote
unit1.pas(110,29) Error: Incompatible type for arg no. 1: Got "<procedure variable type of function(Pointer):Int64 of object;Register>", expected "<procedure variable type of function(Pointer):Int64;Register>"

Lazarus 2.0.10 rUnversioned directory FPC 3.2.0 x86_64-linux-gtk2

~ Dave
You must not pass a Procedure of an Object (In your case Form1)
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

cov

  • Full Member
  • ***
  • Posts: 231
Re: Beginthread incompatible type error
« Reply #2 on: November 20, 2020, 09:44:48 am »
Ah!

Thanks.

cov

  • Full Member
  • ***
  • Posts: 231
Re: Beginthread incompatible type error
« Reply #3 on: November 20, 2020, 10:43:21 am »
Hmm.

Now the error message is
Quote
unit1.pas(122,29) Error: Incompatible type for arg no. 1: Got "<address of function:Int64;Register>", expected "<procedure variable type of function(Pointer):Int64;Register>"

avk

  • Sr. Member
  • ****
  • Posts: 325
    • my self-education project
Re: Beginthread incompatible type error
« Reply #4 on: November 20, 2020, 01:07:56 pm »
Note:
Code: Pascal  [Select][+][-]
  1. ...
  2.   TThreadFunc = function(parameter : pointer) : ptrint;
  3. ...
  4.  

Zvoni

  • Sr. Member
  • ****
  • Posts: 423
Re: Beginthread incompatible type error
« Reply #5 on: November 20, 2020, 01:50:02 pm »
Yep. Function-Argument "parameter:Pointer" is missing in your Function
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

cov

  • Full Member
  • ***
  • Posts: 231
Re: Beginthread incompatible type error
« Reply #6 on: November 20, 2020, 07:45:59 pm »
unit3.pas(15,1) Fatal: Syntax error, "BEGIN" expected but "identifier TTHREADFUNC" found

~ Dave
« Last Edit: November 20, 2020, 09:00:36 pm by cov »

lucamar

  • Hero Member
  • *****
  • Posts: 3236
Re: Beginthread incompatible type error
« Reply #7 on: November 21, 2020, 12:20:11 am »
Would have been nice to see what's in unit3 which generates the error, don't you think? ;)

Anyway, just in case, what they are telling you is that your function should be declared as:
Code: Pascal  [Select][+][-]
  1. function getPedalInput(Param: pointer): ptrint;
if you want to pass it to BeginThread().
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

cov

  • Full Member
  • ***
  • Posts: 231
Re: Beginthread incompatible type error
« Reply #8 on: November 21, 2020, 02:20:05 pm »
Yes, sorry.

Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7.  
  8. uses
  9.   Classes, SysUtils;
  10.  
  11. function getPedalInput:ptrint;
  12. procedure startPedalThread;
  13. implementation
  14. uses unit1;
  15. function getPedalInput:ptrint;
  16. var
  17.   pedalInput: TFileStream;
  18. begin
  19.   pedalInput:=TFileStream.Create('/dev/usb/hiddev0',fmOpenRead);
  20.   while True do begin
  21.     if pedalInput.Size>0 then
  22.     Unit1.Form1.Memo1.Lines.Add('pressed');
  23.   end;
  24.   getPedalInput:=0;
  25. end;
  26.  
  27. procedure startPedalThread;
  28. begin
  29.   BeginThread(@getPedalInput);
  30. end;
  31.  
  32. end.
  33.  

lucamar

  • Hero Member
  • *****
  • Posts: 3236
Re: Beginthread incompatible type error
« Reply #9 on: November 21, 2020, 07:38:35 pm »
There you have it, then; it should be like this:

Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. function getPedalInput(Param: pointer): ptrint;
  11. procedure startPedalThread;
  12.  
  13. implementation
  14.  
  15. uses unit1;
  16.  
  17. function getPedalInput(Param: pointer): ptrint;
  18. var
  19.   pedalInput: TFileStream;
  20. begin
  21.   pedalInput:=TFileStream.Create('/dev/usb/hiddev0',fmOpenRead);
  22.   while True do begin
  23.     if pedalInput.Size>0 then
  24.     Unit1.Form1.Memo1.Lines.Add('pressed');
  25.   end;
  26.   getPedalInput:=0;
  27. end;
  28.  
  29. procedure startPedalThread;
  30. begin
  31.   BeginThread(@getPedalInput);
  32. end;
  33.  
  34. end.

That compiles right.

Thought I don't see where the " 'BEGIN' expected ..." error comes from, to be honest ...

A small nitpicking: since it's called internallly only you don't need to have getPedalInput() declared in the interface section. Other than that, it looks about correct :)

ETA: Of course, you should have a public TThreadId var somewhere to store the result of BeginThread and be able to manage the thread from elsewhere, and getPedalInput() should have some way of exiting the loop and terminate the thread when desired, but I guess this is just a first aproximation and you'll flesh it up some more in the near future, won't you? ;)
« Last Edit: November 21, 2020, 07:48:08 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

cov

  • Full Member
  • ***
  • Posts: 231
Re: Beginthread incompatible type error
« Reply #10 on: November 22, 2020, 07:04:10 am »
Thank you,

It works now!
« Last Edit: November 22, 2020, 08:00:16 am by cov »

cov

  • Full Member
  • ***
  • Posts: 231
Re: Beginthread incompatible type error
« Reply #11 on: November 22, 2020, 07:57:51 am »
Thought I don't see where the " 'BEGIN' expected ..." error comes from, to be honest ...
Sorry, the version I posted didn't have "TThreadFunc = "
Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7.  
  8. uses
  9.   Classes, SysUtils;
  10.  
  11. implementation
  12. uses unit1;
  13. TThreadFunc = function getPedalInput:ptrint;
  14. begin
  15.   getPedalInput:=0;
  16. end;
  17.  
  18. procedure startPedalThread;
  19. begin
  20.   BeginThread(@getPedalInput);
  21. end;
  22.  
  23. end.
  24.  

lucamar

  • Hero Member
  • *****
  • Posts: 3236
Re: [SOLVED]Beginthread incompatible type error
« Reply #12 on: November 22, 2020, 08:07:33 am »
Ah, yes. The TThreadFunc = looks like a type declaration but you're not in a type section so the compiler doesn't expect to find something like that there. Since there is no type, var, const, etc. it expects just code, whether procedure/function definitions or a "main" block starting with "begin"; hence the error message. :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018