Compiler don't know the SELF keyword lazarus / 21-General ---------------------------------------------------------------------- paule32 · 2025-07-07 15:29 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+}unit Stream; interfaceuses  Windows, Dialogs; // --------- ---------------------------------------------------------------------- --------// fm - stream file modes ...// ------------------------------ ---------------------------------------------------------const fmOpenRead        = 0;        // open file for read  onlyconst fmOpenWrite       = 1;        // open file for write onlyconst fmOpenReadWrite   = 2;        // open file for read & writeconst fmCreate          = $FF00;    // create a new file const fmShareCompat     = $00;      // DOS compatible (obsulete)const fmShareExclusive  = $10;      // no other process can access the fileconst fmShareDenyWrite  = $20;      // by other processes: deny read/writeconst fmShareDenyRead   = $30;      // other processes - only read accessconst fmShareDenyNone   = $40;      // read/write access for other processes// ----------------------------------------- ----------------------------------------------const soBeginning       = 0;const soCurrent         = 1;const soEnd             = 2;  type  TStream = class(TObject)  private    FBuffer   : Pointer;    FSize   : Integer;    FCapacity : Integer;    FPosition : Integer;  protected    function  GetSize: Integer;    procedure SetCapacity(AValue: Integer);    procedure ReAlloc    (AValue: Integer);  public    constructor Create(AFileName: String); overload;    constructor Create; overload;        destructor Destroy; override;        procedure ReadBuffer (var   Buffer; Count: Integer);    function  Read       (var   Buffer; Count: Integer): Integer;    function  WriteBuffer(const Buffer; Count: Integer): Integer;    function  Write      (const Buffer; Count: Integer): Integer;        function Seek(Offset: Integer; Origin: Word): Integer;        procedure LoadFromFile(const FileName: string);    procedure LoadFromStream(Source: TStream);        procedure SaveToFile(const FileName: string);    procedure SaveToStream(dest: TStream);      published    property Size     : Integer read FSize;    property Capacity : Integer read FCapacity write SetCapacity;    property Position : Integer read FPosition write FPosition;  end;    TMemoryStream = class(TStream)  public    constructor Create;    destructor Destroy; override;     property Buffer: Pointer read FBuffer;  end;    TFileStream = class(TStream)  public    constructor Create(FileName : String; mode: Integer);    destructor Destroy; override;  end;    TResourceStream = class(TStream)  public    constructor Create;    destructor Destroy; override;  end; implementationuses  Memory, Exceptions; { TStream }constructor TStream.Create(AFileName: String);begin  inherited Create;end; constructor TStream.Create;begin  inherited Create;  FSize     := 0;  FCapacity := 0;  FPosition := 0;end; destructor TStream.Destroy;begin  if FBuffer <> nil then  FreeMem(FBuffer);    inherited Destroy;end; function TStream.Write(const Buffer; Count: LongInt): Integer;var  NewPos: Integer;begin  if Count <= 0 then  Exit(0);   NewPos := FPosition + Count;   // Puffer vergrößern, falls notwendig  if NewPos > FCapacity then  SetCapacity(NewPos);   // Größe anpassen, wenn über das aktuelle Ende hinaus geschrieben wird  if NewPos > FSize then  FSize := NewPos;   // Daten kopieren  Move(Buffer, PByte(Fbuffer)[FPosition], Count);   // Position aktualisieren  inc(FPosition, Count);   // Rückgabe = Anzahl geschriebener Bytes  result := Count;end; function TStream.WriteBuffer(const Buffer; Count: Integer): Integer;var  P: PByte;  BytesWritten, TotalWritten: Integer;begin  P := @Buffer;  TotalWritten := 0;   while Count > 0 do  begin    BytesWritten := Write(P^, Count);        if BytesWritten <= 0 then    Break;        inc(P, BytesWritten);    inc(TotalWritten, BytesWritten);        dec(Count, BytesWritten);  end;   result := TotalWritten;end; procedure TStream.ReadFromFile(const FileName: string);var  FileStream: TFileStream;begin  FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);  try    // Position an das Ende setzen    FileStream.Seek(0, soEnd);    CopyFrom(FileStream, FileStream.Size);  finally    FileStream.Free;  end;end;procedure TStream.SaveToFile(const FileName: string);var  FileStream: TFileStream;begin  FileStream := TFileStream.Create(FileName, fmCreate);  try    SaveToStream(FileStream);  finally    FileStream.Free;  end;end; procedure TStream.SetSize(NewSize: Integer);begin  if NewSize <> self.GetSize then  begin    if NewSize > FCapacity then    SetCapacity(NewSize);        FSize := NewSize;        if FPosition > FSize then    FPosition := FSize;  end;end; procedure TStream.LoadFromStream(Source: TStream);begin  SetSize(0);           // Inhalt löschen  Seek(0, soBeginning);   // an Anfang gehen  CopyFrom(Source, Source.Size); // neuen Inhalt lesen  Seek(0, soBeginning);   // wieder zurückend;procedure TStream.SaveToStream(dest: TStream);begin  if FSize > 0 then  dest.WriteBuffer(FBuffer^, FSize);end; procedure TStream.SetCapacity(AValue: Integer);begin  if AValue <> FCapacity then  ReAllocMemory(FBuffer, AValue);end; procedure TStream.ReAlloc(AValue: Integer);var  NewBuffer: Pointer;begin  if AValue < FSize then  AValue := FSize;   if AValue = 0 then  begin    if FBuffer <> nil then    begin      // alten Inhalt kopieren      Move(FBuffer^, NewBuffer^, FSize);      FreeMem(FBuffer);    end;  end else  begin    if FBuffer = nil then    GetMem(FBuffer, AValue) else  ReAllocMemory(FBuffer)  end;   FBuffer   := NewBuffer;  FCapacity := NewCapacity;end; procedure TStream.ReadBuffer(var Buffer; Count: LongInt);var  P: PByte;  BytesRead, Remaining: LongInt;begin  if Count <= 0 then  exit;   P := @Buffer;  Remaining := Count;   while Remaining > 0 do  begin    BytesRead := Read(P^, Remaining);    if BytesRead <= 0 then    raise EReadError.Create('Fehler beim Lesen aus dem Stream');     Inc(P, BytesRead);    Dec(Remaining, BytesRead);  end;end; function TStream.Read(var Buffer; Count: Integer): Integer;begin  if FPosition >= FSize then  Exit(0);    if FPosition + Count > FSize then  Count := FSize - FPosition;    Move(PByte(FBuffer)^[FPosition], Buffer, Count);  inc(FPosition), Count);    result := Count;end; function TStream.Write(const Buffer; Count: Integer): Integer;var  NewPos: LongInt;begin  NewPos := FPosition + Count;    if NewPos > FCapacity then  SetCapacity(NewPos * 2); // wächst dynamisch   Move(Buffer, PByte(FBuffer)^[FPosition], Count);  FPosition := NewPos;    if FPosition > FSize then  FSize  := FPosition;    result := Count;end; function TStream.Seek(Offset: Integer; Origin: Word): Integer;begin  case Origin of    soBeginning: FPosition := Offset;    soCurrent:   FPosition := FPosition + Offset;    soEnd:       FPosition := FSize     + Offset;  else    raise Exception.Create('invalid Seek-Offset');  end;   if FPosition < 0 then  FPosition := 0 else if FPosition > FSize then  FPosition := FSize;   result    := FPosition;end;  { TMemoryStream }constructor TMemoryStream.Create;begin  inherited Create;  FBuffer   := nil;  FSize     := 0;  FCapacity := 0;  FPosition := 0;end; destructor TMemoryStream.Destroy;begin  if FBuffer <> nil then  FreeMem(FBuffer);  inherited Destroy;end;  { TFileStream }constructor TFileStream.Create;begin  inherited Create;end; destructor TFileStream.Destroy;begin  inherited Destroy;end;  { TResourceStream }constructor TResourceStream.Create;begin  inherited Create;end; destructor TResourceStream.Destroy;begin  inherited Destroy;end; end. The Output will be: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Stream.pas(189,17) Error: Identifier not found "self" ---------------------------------------------------------------------- olatov · 2025-07-07 15:40 You don't have SetSize method in your class declaration. Why are you reimplementing the streams anyway? ---------------------------------------------------------------------- Thaddy · 2025-07-07 16:01 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. ---------------------------------------------------------------------- Nitorami · 2025-07-07 17:06 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 · 2025-07-07 17:16 If you want help, make sure your posted sourcecode is compilable. ---------------------------------------------------------------------- jamie · 2025-07-07 22:37 Must of been a late night Jamie ---------------------------------------------------------------------- Thaddy · 2025-07-08 04:38 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. ---------------------------------------------------------------------- paule32 · 2025-07-08 06:02 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 ---------------------------------------------------------------------- H₂SO₄ · 2025-07-08 06:11 Quote from: paule32 on July 08, 2025, 08:02:33 amit is not of my Questions how many Compiler Errors occur. It is the keyword SELF that will not recognized. @Nimbus already answered your question: Quote from: Nimbus on July 07, 2025, 05:40:25 pmYou 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).