Recent

Author Topic: Compiler don't know the SELF keyword  (Read 1063 times)

paule32

  • Hero Member
  • *****
  • Posts: 645
  • One in all. But, not all in one.
Compiler don't know the SELF keyword
« on: July 07, 2025, 05:29:41 pm »
I have the following Code, which can't be compiled because FPC 3.2.2 don't can resolve the SELF keyword in Line: 189.
So, the Question is, why is that so ?

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. unit Stream;
  3.  
  4. interface
  5. uses
  6.   Windows, Dialogs;
  7.  
  8. // ---------------------------------------------------------------------------------------
  9. // fm - stream file modes ...
  10. // ---------------------------------------------------------------------------------------
  11. const fmOpenRead        = 0;        // open file for read  only
  12. const fmOpenWrite       = 1;        // open file for write only
  13. const fmOpenReadWrite   = 2;        // open file for read & write
  14. const fmCreate          = $FF00;    // create a new file
  15.  
  16. const fmShareCompat     = $00;      // DOS compatible (obsulete)
  17. const fmShareExclusive  = $10;      // no other process can access the file
  18. const fmShareDenyWrite  = $20;      // by other processes: deny read/write
  19. const fmShareDenyRead   = $30;      // other processes - only read access
  20. const fmShareDenyNone   = $40;      // read/write access for other processes
  21. // ---------------------------------------------------------------------------------------
  22. const soBeginning       = 0;
  23. const soCurrent         = 1;
  24. const soEnd             = 2;
  25.  
  26. type
  27.   TStream = class(TObject)
  28.   private
  29.     FBuffer   : Pointer;
  30.     FSize     : Integer;
  31.     FCapacity : Integer;
  32.     FPosition : Integer;
  33.   protected
  34.     function  GetSize: Integer;
  35.     procedure SetCapacity(AValue: Integer);
  36.     procedure ReAlloc    (AValue: Integer);
  37.   public
  38.     constructor Create(AFileName: String); overload;
  39.     constructor Create; overload;
  40.    
  41.     destructor Destroy; override;
  42.    
  43.     procedure ReadBuffer (var   Buffer; Count: Integer);
  44.     function  Read       (var   Buffer; Count: Integer): Integer;
  45.     function  WriteBuffer(const Buffer; Count: Integer): Integer;
  46.     function  Write      (const Buffer; Count: Integer): Integer;
  47.    
  48.     function Seek(Offset: Integer; Origin: Word): Integer;
  49.    
  50.     procedure LoadFromFile(const FileName: string);
  51.     procedure LoadFromStream(Source: TStream);
  52.    
  53.     procedure SaveToFile(const FileName: string);
  54.     procedure SaveToStream(dest: TStream);
  55.    
  56.   published
  57.     property Size     : Integer read FSize;
  58.     property Capacity : Integer read FCapacity write SetCapacity;
  59.     property Position : Integer read FPosition write FPosition;
  60.   end;
  61.  
  62.   TMemoryStream = class(TStream)
  63.   public
  64.     constructor Create;
  65.     destructor Destroy; override;
  66.  
  67.     property Buffer: Pointer read FBuffer;
  68.   end;
  69.  
  70.   TFileStream = class(TStream)
  71.   public
  72.     constructor Create(FileName : String; mode: Integer);
  73.     destructor Destroy; override;
  74.   end;
  75.  
  76.   TResourceStream = class(TStream)
  77.   public
  78.     constructor Create;
  79.     destructor Destroy; override;
  80.   end;
  81.  
  82. implementation
  83. uses
  84.   Memory, Exceptions;
  85.  
  86. { TStream }
  87. constructor TStream.Create(AFileName: String);
  88. begin
  89.   inherited Create;
  90. end;
  91.  
  92. constructor TStream.Create;
  93. begin
  94.   inherited Create;
  95.   FSize     := 0;
  96.   FCapacity := 0;
  97.   FPosition := 0;
  98. end;
  99.  
  100. destructor TStream.Destroy;
  101. begin
  102.   if FBuffer <> nil then
  103.   FreeMem(FBuffer);
  104.  
  105.   inherited Destroy;
  106. end;
  107.  
  108. function TStream.Write(const Buffer; Count: LongInt): Integer;
  109. var
  110.   NewPos: Integer;
  111. begin
  112.   if Count <= 0 then
  113.   Exit(0);
  114.  
  115.   NewPos := FPosition + Count;
  116.  
  117.   // Puffer vergrößern, falls notwendig
  118.   if NewPos > FCapacity then
  119.   SetCapacity(NewPos);
  120.  
  121.   // Größe anpassen, wenn über das aktuelle Ende hinaus geschrieben wird
  122.   if NewPos > FSize then
  123.   FSize := NewPos;
  124.  
  125.   // Daten kopieren
  126.   Move(Buffer, PByte(Fbuffer)[FPosition], Count);
  127.  
  128.   // Position aktualisieren
  129.   inc(FPosition, Count);
  130.  
  131.   // Rückgabe = Anzahl geschriebener Bytes
  132.   result := Count;
  133. end;
  134.  
  135. function TStream.WriteBuffer(const Buffer; Count: Integer): Integer;
  136. var
  137.   P: PByte;
  138.   BytesWritten, TotalWritten: Integer;
  139. begin
  140.   P := @Buffer;
  141.   TotalWritten := 0;
  142.  
  143.   while Count > 0 do
  144.   begin
  145.     BytesWritten := Write(P^, Count);
  146.    
  147.     if BytesWritten <= 0 then
  148.     Break;
  149.    
  150.     inc(P, BytesWritten);
  151.     inc(TotalWritten, BytesWritten);
  152.    
  153.     dec(Count, BytesWritten);
  154.   end;
  155.  
  156.   result := TotalWritten;
  157. end;
  158.  
  159. procedure TStream.ReadFromFile(const FileName: string);
  160. var
  161.   FileStream: TFileStream;
  162. begin
  163.   FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  164.   try
  165.     // Position an das Ende setzen
  166.     FileStream.Seek(0, soEnd);
  167.     CopyFrom(FileStream, FileStream.Size);
  168.   finally
  169.     FileStream.Free;
  170.   end;
  171. end;
  172. procedure TStream.SaveToFile(const FileName: string);
  173. var
  174.   FileStream: TFileStream;
  175. begin
  176.   FileStream := TFileStream.Create(FileName, fmCreate);
  177.   try
  178.     SaveToStream(FileStream);
  179.   finally
  180.     FileStream.Free;
  181.   end;
  182. end;
  183.  
  184. procedure TStream.SetSize(NewSize: Integer);
  185. begin
  186.   if NewSize <> self.GetSize then
  187.   begin
  188.     if NewSize > FCapacity then
  189.     SetCapacity(NewSize);
  190.    
  191.     FSize := NewSize;
  192.    
  193.     if FPosition > FSize then
  194.     FPosition := FSize;
  195.   end;
  196. end;
  197.  
  198. procedure TStream.LoadFromStream(Source: TStream);
  199. begin
  200.   SetSize(0);             // Inhalt löschen
  201.   Seek(0, soBeginning);   // an Anfang gehen
  202.   CopyFrom(Source, Source.Size); // neuen Inhalt lesen
  203.   Seek(0, soBeginning);   // wieder zurück
  204. end;
  205. procedure TStream.SaveToStream(dest: TStream);
  206. begin
  207.   if FSize > 0 then
  208.   dest.WriteBuffer(FBuffer^, FSize);
  209. end;
  210.  
  211. procedure TStream.SetCapacity(AValue: Integer);
  212. begin
  213.   if AValue <> FCapacity then
  214.   ReAllocMemory(FBuffer, AValue);
  215. end;
  216.  
  217. procedure TStream.ReAlloc(AValue: Integer);
  218. var
  219.   NewBuffer: Pointer;
  220. begin
  221.   if AValue < FSize then
  222.   AValue := FSize;
  223.  
  224.   if AValue = 0 then
  225.   begin
  226.     if FBuffer <> nil then
  227.     begin
  228.       // alten Inhalt kopieren
  229.       Move(FBuffer^, NewBuffer^, FSize);
  230.       FreeMem(FBuffer);
  231.     end;
  232.   end else
  233.   begin
  234.     if FBuffer = nil then
  235.     GetMem(FBuffer, AValue) else
  236.     ReAllocMemory(FBuffer)
  237.   end;
  238.  
  239.   FBuffer   := NewBuffer;
  240.   FCapacity := NewCapacity;
  241. end;
  242.  
  243. procedure TStream.ReadBuffer(var Buffer; Count: LongInt);
  244. var
  245.   P: PByte;
  246.   BytesRead, Remaining: LongInt;
  247. begin
  248.   if Count <= 0 then
  249.   exit;
  250.  
  251.   P := @Buffer;
  252.   Remaining := Count;
  253.  
  254.   while Remaining > 0 do
  255.   begin
  256.     BytesRead := Read(P^, Remaining);
  257.     if BytesRead <= 0 then
  258.     raise EReadError.Create('Fehler beim Lesen aus dem Stream');
  259.  
  260.     Inc(P, BytesRead);
  261.     Dec(Remaining, BytesRead);
  262.   end;
  263. end;
  264.  
  265. function TStream.Read(var Buffer; Count: Integer): Integer;
  266. begin
  267.   if FPosition >= FSize then
  268.   Exit(0);
  269.  
  270.   if FPosition + Count > FSize then
  271.   Count := FSize - FPosition;
  272.  
  273.   Move(PByte(FBuffer)^[FPosition], Buffer, Count);
  274.   inc(FPosition), Count);
  275.  
  276.   result := Count;
  277. end;
  278.  
  279. function TStream.Write(const Buffer; Count: Integer): Integer;
  280. var
  281.   NewPos: LongInt;
  282. begin
  283.   NewPos := FPosition + Count;
  284.  
  285.   if NewPos > FCapacity then
  286.   SetCapacity(NewPos * 2); // wächst dynamisch
  287.  
  288.   Move(Buffer, PByte(FBuffer)^[FPosition], Count);
  289.   FPosition := NewPos;
  290.  
  291.   if FPosition > FSize then
  292.   FSize  := FPosition;
  293.  
  294.   result := Count;
  295. end;
  296.  
  297. function TStream.Seek(Offset: Integer; Origin: Word): Integer;
  298. begin
  299.   case Origin of
  300.     soBeginning: FPosition := Offset;
  301.     soCurrent:   FPosition := FPosition + Offset;
  302.     soEnd:       FPosition := FSize     + Offset;
  303.   else
  304.     raise Exception.Create('invalid Seek-Offset');
  305.   end;
  306.  
  307.   if FPosition < 0 then
  308.   FPosition := 0 else if FPosition > FSize then
  309.   FPosition := FSize;
  310.  
  311.   result    := FPosition;
  312. end;
  313.  
  314.  
  315. { TMemoryStream }
  316. constructor TMemoryStream.Create;
  317. begin
  318.   inherited Create;
  319.   FBuffer   := nil;
  320.   FSize     := 0;
  321.   FCapacity := 0;
  322.   FPosition := 0;
  323. end;
  324.  
  325. destructor TMemoryStream.Destroy;
  326. begin
  327.   if FBuffer <> nil then
  328.   FreeMem(FBuffer);
  329.   inherited Destroy;
  330. end;
  331.  
  332.  
  333. { TFileStream }
  334. constructor TFileStream.Create;
  335. begin
  336.   inherited Create;
  337. end;
  338.  
  339. destructor TFileStream.Destroy;
  340. begin
  341.   inherited Destroy;
  342. end;
  343.  
  344.  
  345. { TResourceStream }
  346. constructor TResourceStream.Create;
  347. begin
  348.   inherited Create;
  349. end;
  350.  
  351. destructor TResourceStream.Destroy;
  352. begin
  353.   inherited Destroy;
  354. end;
  355.  
  356. end.

The Output will be:

Code: Bash  [Select][+][-]
  1. Stream.pas(189,17) Error: Identifier not found "self"
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Nimbus

  • Jr. Member
  • **
  • Posts: 85
Re: Compiler don't know the SELF keyword
« Reply #1 on: July 07, 2025, 05:40:25 pm »
You don't have SetSize method in your class declaration. Why are you reimplementing the streams anyway?

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: Compiler don't know the SELF keyword
« Reply #2 on: July 07, 2025, 06:01:18 pm »
Self is from TObject. If you implemented your version of TObject correctly you also have self: classes work like that. self means simply a reference to itself. You won't find any fields for it. It is a reference to its own pointer.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: Compiler don't know the SELF keyword
« Reply #3 on: July 07, 2025, 07:06:18 pm »
When trying to compile this, I get many errors before even getting to the "self" issue:

Can't find unit exceptions
line "procedure TStream.ReadFromFile(const FileName: string);": Method identifier expected
line "procedure TStream.SetSize(NewSize: Integer);" method identifier expected
Identifier not found "CopyFrom"
line "ReAllocMemory(FBuffer)": Wrong number of parameters

etc. etc.




marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12706
  • FPC developer.
Re: Compiler don't know the SELF keyword
« Reply #4 on: July 07, 2025, 07:16:15 pm »
If you want help, make sure your posted sourcecode is compilable.

jamie

  • Hero Member
  • *****
  • Posts: 7589
Re: Compiler don't know the SELF keyword
« Reply #5 on: July 08, 2025, 12:37:15 am »
Must of been a late night :o

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: Compiler don't know the SELF keyword
« Reply #6 on: July 08, 2025, 06:38:35 am »
I think @nitorami put you on the right track: if exceptions are not implemented you need to handle that in a different way: old school. Although the fact that self is not recognized is a different error: self is a pointer to the instance. Nothing more.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

paule32

  • Hero Member
  • *****
  • Posts: 645
  • One in all. But, not all in one.
Re: Compiler don't know the SELF keyword
« Reply #7 on: July 08, 2025, 08:02:33 am »
it is not of my Questions how many Compiler Errors occur.
It is the keyword SELF that will not recognized.

there the source:
https://github.com/minifpc/miniRTL/tree/A2/src
« Last Edit: July 08, 2025, 08:06:22 am by paule32 »
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Khrys

  • Sr. Member
  • ****
  • Posts: 400
Re: Compiler don't know the SELF keyword
« Reply #8 on: July 08, 2025, 08:11:30 am »
it is not of my Questions how many Compiler Errors occur.
It is the keyword SELF that will not recognized.

@Nimbus already answered your question:

You don't have SetSize method in your class declaration.

The compiler doesn't expect  Self  anywhere in the implementation since it does not know that  SetSize  is supposed to be a method and not a free-standing function.
You'd know this if you had read the preceding compiler errors (as pointed out by @Nitorami).

 

TinyPortal © 2005-2018