gpt4 book ai didi

pascal - 在 Pascal 中创建一个类

转载 作者:行者123 更新时间:2023-12-02 02:33:20 26 4
gpt4 key购买 nike

我正在尝试在 Pascal 中创建一个类,我对声明和语法有点困惑。主要的事情是一个错误,我收到“前向声明未解决 Tetromino.Rotate(LongInt)”,我读到我需要在实现部分声明我的过程,但我不确定在哪里我本来就是要这么说的。另外,如果您发现我的类(class)声明有任何其他问题,请告诉我。

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.

最佳答案

在程序模块中,无需划分接口(interface)实现。因此,错误描述(用于实现实现部分中的过程)有点误导。尽管如此,它仍然表明缺少 Rotate() 过程的实现。

因此,错误在于您已在 Tetromino 类中声明了一个过程,但缺少该过程的实现。您需要在类声明和程序的 begin .. end block 之间的某个位置实现它。

unit 模块中,该模块具有命名部分:interfaceimplementation,您可以在 interface 中声明类> 部分(如果这些类可以从其他模块访问)并在 implementation 部分中实现它们。

下面我概述了您需要在程序中执行的操作,包括 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.

关于pascal - 在 Pascal 中创建一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64724994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com