Recent

Author Topic: Object orientation question  (Read 4690 times)

deavmi

  • Newbie
  • Posts: 3
Object orientation question
« on: January 20, 2022, 01:57:09 pm »
I have been reading through the reference manual for the Free Pascal compiler and I have come across the page on Object orientation (page 76) yet the example given doesn't compile:

Code: Pascal  [Select][+][-]
  1. Type
  2. TObj = object
  3. Private
  4. Caption : ShortString;
  5. Public
  6. Constructor init;
  7. Destructor done;
  8. Procedure SetCaption (AValue : String);
  9. Function GetCaption : String;
  10. end;
  11.  

I have run the following command:

Code: Pascal  [Select][+][-]
  1. deavmi@bababooey:~/Documents/Pascal $ fpc -Cr -Sc hello3.pas &&
  2. ./hello3
  3.  

And received the following output:

Code: Pascal  [Select][+][-]
  1. Free Pascal Compiler version 3.2.2 [2021/09/21] for x86_64
  2. Copyright (c) 1993-2021 by Florian Klaempfl and others
  3. Target OS: Linux for x86-64
  4. Compiling hello3.pas
  5. hello3.pas(14,1) Note: Private field "TObj.Caption" is never used
  6. hello3.pas(16,13) Error: Forward declaration not solved "constructor
  7. init;"
  8. hello3.pas(17,12) Error: Forward declaration not solved "destructor
  9. done;"
  10. hello3.pas(18,11) Error: Forward declaration not solved
  11. "SetCaption(ShortString);"
  12. hello3.pas(19,10) Error: Forward declaration not solved
  13. "GetCaption:ShortString;"
  14. hello3.pas(24,4) Fatal: There were 4 errors compiling module, stopping
  15. Fatal: Compilation aborted
  16. Error: /usr/bin/ppcx64 returned an error exitcode
  17.  



Is there something I am missing, I would be greatly appreciative of any input :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Object orientation question
« Reply #1 on: January 20, 2022, 02:38:16 pm »
Read the error messages, they're telling you what you need to provide.

If you can't work it out, then attach your code and tell us what OS etc. you're using. Also specify exactly where you've found the info which you believe is incorrect.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Object orientation question
« Reply #2 on: January 20, 2022, 02:44:34 pm »
Read the error messages, they're telling you what you need to provide.

If you can't work it out, then attach your code and tell us what OS etc. you're using. Also specify exactly where you've found the info which you believe is incorrect.

MarkMLl
Probably this one: https://www.freepascal.org/docs-html/current/ref/refse28.html#x60-800005.1
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

deavmi

  • Newbie
  • Posts: 3
Re: Object orientation question
« Reply #3 on: January 20, 2022, 02:57:14 pm »
Read the error messages, they're telling you what you need to provide.

If you can't work it out, then attach your code and tell us what OS etc. you're using. Also specify exactly where you've found the info which you believe is incorrect.

MarkMLl

OS is Linux.

I'm pretty new to Pascal, I'm familiar with OOP in lnaguages like Java, D etc. So does the declaration of the type not include the declaration or what? I can see what the error message says about a forward declaration but where should one "implement" it then, the documentation wasn't all that helpful in the reference manual w.r.t. to that.

Page 76 of this doucment: ftp://ftp.freepascal.org/pub/fpc/docs-pdf/ref.pdf
« Last Edit: January 20, 2022, 02:59:39 pm by deavmi »

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Object orientation question
« Reply #4 on: January 20, 2022, 03:05:56 pm »
I can see what the error message says about a forward declaration but where should one "implement" it then, the documentation wasn't all that helpful in the reference manual w.r.t. to that.

--> https://www.freepascal.org/docs-html/current/ref/refse112.html
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Object orientation question
« Reply #5 on: January 20, 2022, 03:19:36 pm »
Read the error messages, they're telling you what you need to provide.

If you can't work it out, then attach your code and tell us what OS etc. you're using. Also specify exactly where you've found the info which you believe is incorrect.

MarkMLl

OS is Linux.

I'm pretty new to Pascal, I'm familiar with OOP in lnaguages like Java, D etc. So does the declaration of the type not include the declaration or what? I can see what the error message says about a forward declaration but where should one "implement" it then, the documentation wasn't all that helpful in the reference manual w.r.t. to that.

Page 76 of this doucment: ftp://ftp.freepascal.org/pub/fpc/docs-pdf/ref.pdf

In Free Pascal there is 'Class' and there is 'Object', you can read more about it here https://wiki.freepascal.org/Class and here https://wiki.freepascal.org/Object.

You probably want to use Class, and not Object. In the first link you have some examples, and at the end is a comparison of structured types in Free Pascal.

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: Object orientation question
« Reply #6 on: January 20, 2022, 03:28:16 pm »
In Object Pascal, a unit is the regular source file where you both declare and define your routines and variables.

A unit starts with the Interface section and after that comes the implementation section. Find the keywords "interface" and "implementation" in the example below.

Routines that are declared in the interface section but not defined will make the unit not compile. Thats whats missing in your code example. Which btw has naming conventions that I dont think anyone uses anymore. Look at this instead.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.  
  12.  { TObj }
  13.  
  14.  TObj = class
  15.  private
  16.    FCaption : ShortString;
  17.  public
  18.    Constructor Create;
  19.    Destructor Destroy; override;
  20.    Procedure SetCaption (AValue : String);
  21.    Function GetCaption : String;
  22.  end;
  23.  
  24.  
  25. implementation
  26.  
  27. { TObj }
  28.  
  29. constructor TObj.Create;
  30. begin
  31.   ;
  32. end;
  33.  
  34. destructor TObj.Destroy;
  35. begin
  36.   ;
  37. end;
  38.  
  39. procedure TObj.SetCaption(AValue: String);
  40. begin
  41.   FCaption:=AValue;
  42. end;
  43.  
  44. function TObj.GetCaption: String;
  45. begin
  46.   Result:= FCaption;
  47. end;
  48.  
  49. end.
  50.  
  51.  
« Last Edit: January 20, 2022, 06:57:33 pm by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

deavmi

  • Newbie
  • Posts: 3
Re: Object orientation question
« Reply #7 on: January 20, 2022, 03:52:55 pm »
Thanks to the last two posters, this clears it up for me and I know understand how it is done in Pascal.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Object orientation question
« Reply #8 on: January 20, 2022, 06:41:02 pm »
Thanks to the last two posters, this clears it up for me and I know understand how it is done in Pascal.

Sorry if I was a bit abrupt earlier, but I was trying to get you to look and think :-)

Noting obviously what everybody has said about "class" vs "object"- which is an historical peculiarity- there's two things here.

First, unless you're specifically writing a single-unit program then each unit has an interface part and an implementation part. This is particularly apparent if you're using the Lazarus RAD IDE, where the main program is basically a stub "project" file.

Second, any class/object has a type declaration, and then a separate definition of the code that actually implements the constructor etc. Very often, the declaration will be in the interface part of a unit and the implementation will be hidden away in the implementation part.

And if you look at the error messages carefully, you'll see that they're actually "declaration not solved", i.e. you've declared something but not implemented it.

Finally, my reference above to "interface part" is distinct from "interfaces" as an object-oriented concept. The terminology is unfortunate, blame Borland.

HTH.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

speter

  • Sr. Member
  • ****
  • Posts: 345
Re: Object orientation question
« Reply #9 on: January 21, 2022, 12:30:55 am »
To answer the original question, the problem (as pointed out by MarkMLI) is that the object's methods have not been written.

The following code is an example UNIT implementing your object. Note that this is a UNIT (not a PROGRAM).
I have attached the Lazarus Project to this message.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.   TObj = object
  12.     private
  13.       caption : string;
  14.     public
  15.       constructor init;
  16.       destructor done;
  17.       procedure SetCaption(avalue : string);
  18.       function GetCaption : string;
  19.   end;
  20.  
  21.   { TForm1 }
  22.  
  23.   TForm1 = class(TForm)
  24.     Button1: TButton;
  25.     Memo1: TMemo;
  26.     Panel1: TPanel;
  27.     procedure Button1Click(Sender: TObject);
  28.   private
  29.  
  30.   public
  31.     foo : TObj;
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. constructor TObj.init;
  42. begin
  43.   // initialise the object's data
  44.   caption := 'initialised';
  45. end;
  46.  
  47. destructor TObj.done;
  48. begin
  49.   // de-allocate any memory used etc
  50. end;
  51.  
  52. procedure TObj.SetCaption(avalue : string);
  53. begin
  54.   caption := avalue;
  55. end;
  56.  
  57. function TObj.GetCaption : string;
  58. begin
  59.   result := caption;
  60. end;
  61.  
  62. { TForm1 }
  63.  
  64. procedure TForm1.Button1Click(Sender: TObject);
  65. begin
  66.   memo1.append(foo.GetCaption);  // the object already exists and is usable
  67.   foo.init;                      // initialise the object's data
  68.   memo1.append(foo.GetCaption);  // write the caption to a memo
  69.   foo.SetCaption('Hello World'); // change the caption
  70.   memo1.append(foo.GetCaption);  // write the caption to a memo
  71. end;
  72.  
  73. end.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Object orientation question
« Reply #10 on: January 21, 2022, 05:53:10 am »
Same, but as a console program. A bare minimum. Just in case:
Code: Pascal  [Select][+][-]
  1. program objecttest;
  2. {$ifdef mswindows}{$apptype console}{$endif}
  3. {$mode objfpc}{$H+}
  4. type
  5.   TObj = object
  6.     private
  7.       caption : string;
  8.     public
  9.       constructor init;
  10.       destructor done;
  11.       procedure SetCaption(avalue : string);
  12.       function GetCaption : string;
  13.   end;
  14.  
  15. constructor TObj.init;
  16. begin
  17.   caption := 'initialised';
  18. end;
  19.  
  20. destructor TObj.done;
  21. begin
  22.   // de-allocate any memory used etc
  23. end;
  24.  
  25. procedure TObj.SetCaption(avalue : string);
  26. begin
  27.   caption := avalue;
  28. end;
  29.  
  30. function TObj.GetCaption : string;
  31. begin
  32.   result := caption;
  33. end;
  34.  
  35. var
  36.   foo:TObj;
  37. begin
  38.   writeln(foo.GetCaption);       // the object already exists and is usable
  39.   foo.init;                      // initialize the object's data
  40.   writeln(foo.GetCaption);       // write the caption to the console
  41.   foo.SetCaption('Hello World'); // change the caption
  42.   writeln(foo.GetCaption);       // write the caption to the console
  43. end.

« Last Edit: January 21, 2022, 10:06:24 am by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Object orientation question
« Reply #11 on: January 21, 2022, 04:11:43 pm »
I have been reading through the reference manual for the Free Pascal compiler and I have come across the page on Object orientation (page 76) yet the example given doesn't compile:

In addition to what the others wrote: the examples in the manuals are not intended as full examples, but merely to highlight the syntax. They're merely snippets.

 

TinyPortal © 2005-2018