Home > Software design >  Lazarus Error: Anonymous Class definitions are not allowed
Lazarus Error: Anonymous Class definitions are not allowed

Time:06-13

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?

CodePudding user response:

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;

CodePudding user response:

There are few problems in your code.

  1. When creating array of some type you don't define the said type in the array itself but only tell which type needs to be used. So your array definition would be:

    Hand: array of TKarte;

  2. I also see that you have declared all your procedures as class procedure. There is a fundamental difference between class methods and ordinary methods. Most likely you won't need them to be declared as class methods based on what you are trying to achieve. While I could not find suitable Lazarus documentation on this topic you may refer to Delphi documentation on Class methods to get better understanding about their difference.

  • Related