Creating a class in Pascal

Viewed 394

I'm attempting to create a class in Pascal, I am a bit confused about the declaration and syntax. the main thing is an error I'm getting "Forward declaration not solved Tetromino.Rotate(LongInt)", I read that I need to declare my procedure in the implementation section but I'm not sure where I'm meant to be putting that. also if you notice anything else wrong with my class declaration please tell me.

program Tetris;
{$MODE OBJFPC}
uses crt, sysutils;
type 
    Tetromino = class
    
    private
        TempFace : array [0..15] of char;       
    public
        Face : array[0..15] of char;
        //constructor create();  (idk what this is but read somewhere that you need it)
        procedure Rotate(rotation : integer);
    end;
var
    a,b,c,d,e,f,g : tetromino;
begin
    ReadKey();
end. 
1 Answers

In a program module there is no need for division into interface and implementation. Therefore the error description (to implement the procedure in the implementation section) is a little bit misleading. Still, it indicates that the implementation of the Rotate() procedure is missing.

So, the error is that you have declared a procedure in the Tetromino class, but the implementation of that procedure is missing. You need to implement it somewhere between the class declaration and the begin .. end block of the program.

In a unit module, which has named sections: interface and implementation, you declare classes in the interface section (if those classes are to be accessible from other modules) and implement them in the implementation section.

In the following I outline what you need to do in your program, including the constructor for Tetromino

program Tetris;
{$MODE OBJFPC}
uses crt, sysutils;
type 
    Tetromino = class
    private
        TempFace : array [0..15] of char;       
    public
        Face : array[0..15] of char;
        constructor create();  (idk what this is but read somewhere that you need it)
        procedure Rotate(rotation : integer);
    end;

var
    a,b,c,d,e,f,g : tetromino;

constructor Tetromino.create;
begin
  // constructor (automatically) aquires a block of memory
  // to hold members of the class
  // to do: initialize member fields of the instance
end;

procedure Tetromino.Rotate(rotation: Integer);
begin
  // implementation of the Rotate() method
end;

begin
    ReadKey();
end. 
Related