gpt4 book ai didi

delphi - 如何声明一个函数,该函数采用尚未识别的类型的变量作为参数

转载 作者:行者123 更新时间:2023-12-03 18:20:57 25 4
gpt4 key购买 nike

问题如标题所示。 O 收到错误:预期类型标识符,(我认为)来自在类型声明之前读取的函数声明。

type
TForm1 = class(TForm)
//other declarations
function ItemThere(x: TItem): boolean; // here it says TItem is unknown
end;

type
TItem = class
Name : String;
Description : String;
constructor Create;
end;

另外我想让你知道我是一个相当缺乏经验的程序员。我怎样才能解决这个问题?我应该将 TItem 的声明移到 TForm1 上方吗?感谢您的帮助。

最佳答案

Delphi 编译器在使用之前需要了解类型。有两种方法可以使用您的代码来实现此目的。

  1. 将声明移至首次使用的位置上方:

    type
    TItem = class
    Name : String;
    Description : String;
    constructor Create;
    end;

    TForm1 = class(TForm)
    //other declarations
    function ItemThere(x: TItem): boolean;
    end;
  2. 使用所谓的 forward declaration ,它基本上只是告诉编译器您正在使用稍后将定义的类(在同一类型声明部分中):

    type
    TItem = class; // forward declaration

    TForm1 = class(TForm)
    //other declarations
    function ItemThere(x: TItem): boolean;
    end;

    TItem = class // Now define the class itself
    Name : String;
    Description : String;
    constructor Create;
    end;

关于delphi - 如何声明一个函数,该函数采用尚未识别的类型的变量作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40294034/

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