Search code examples
delphigame-developmentlazarus

Lazarus Error: Anonymous Class definitions are not allowed


I have to develop a game in Lazarus for school, and I ran into an error that I can't find a solution for.

I have a dynamic array where I want to store classes in so that I can call procedures on those classes.

TKarte is the ancestor class, and I have many different classes (all representing different Cards) that have the same procedures as the ancestor class.

unit Karten;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, Dialogs, ExtCtrls;

type
  TKarte=class
    public
      class procedure GetPicture(Objekt:TImage);virtual;
      class procedure OnPlay;virtual;
  end;

type
  Karte = class(TKarte)
    public
      class procedure GetPicture(Objekt:TImage);override;
      class procedure OnPlay;override;
end;

type
  Karte2 = class(TKarte)
    public
      class procedure GetPicture(Objekt:TImage);override;
      class procedure OnPlay;override;
  end;

implementation

class procedure Karte.OnPlay();
begin
  ShowMessage(ClassName);
end;

class procedure Karte.GetPicture(Objekt:Timage);
begin
  Objekt.Picture.LoadFromFile('Grafiken\Karten\Mindcontrol.png');
end;

class procedure Karte2.GetPicture(Objekt:Timage);
begin
  Objekt.Picture.LoadFromFile('Grafiken\Karten\Mindcontrol.png');
end;

class procedure Karte2.OnPlay();
begin
  ShowMessage(Karte2.ClassName);
end;

class procedure TKarte.OnPlay();
begin
  ShowMessage(ClassName);
end;

class procedure TKarte.GetPicture(Objekt:TImage);
begin
  Objekt.Picture.LoadFromFile('Grafiken\Sprites\Buttons\Button 1.png');
end;

end. 

This is how I add them and call them from the array at the moment:

Hand: array of Class of TKarte;
procedure TSplashScreen.Button2Click(Sender: TObject);
begin
  SetLength(Hand,Length(Hand)+1);
  Hand[High(Hand)] := Karte;

  Hand[High(Hand)].OnPlay();
  Hand[High(Hand)].GetPicture(Image1);
end;

There is no problem with running the program, but when I try to add a new component, or I press CTRL + Space for the Auto-Complete, it gives me an error at the declaration of the array:

Error: Anonymous Class definitions are not allowed

I have tried to find an answer to this problem, but there seems to be noone with the same problem :(

Can somebody help me?


Solution

  • Offhand, I see nothing wrong with the code, and as you said the code does run correctly. It is only the IDE that is having a problem with it. As such, I would not suggest declaring the array's element type directly in the array's declaration. I would suggest declaring an alias for it before declaring the array, eg:

    type
      TKarte=class
        ...
      end; 
      TKarteClass = Class of TKarte;
    ...
    
    Hand: array of TKarteClass;