gpt4 book ai didi

delphi - 这个不兼容的类型错误怎么了

转载 作者:行者123 更新时间:2023-12-03 19:31:45 24 4
gpt4 key购买 nike

我几天去了,有人问我为什么这段代码给出了错误。我试图弄清楚,但我不能,所以我希望你们能帮助我们。

第一个单元:定义类的位置
单位Unit1;

interface

type
TSomeGeneric<T> = class
private
function getSelf: TSomeGeneric<T>;
public
property This : TSomeGeneric<T> read getSelf;
end;

implementation

{ TSomeGeneric<T> }

function TSomeGeneric<T>.getSelf: TSomeGeneric<T>;
begin
Result := Self;
end;

end.


unit2:它引用在单元1中定义的类
单位Unit2;

interface

uses
Unit1;

type
TSomeGeneric<T> = class(Unit1.TSomeGeneric<T>);

implementation

end.


frmmain:它使用unit2是一点
单位ufrmMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,

Unit2;

type
TfrmMain = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure SomeProcedure(ASG : TSomeGeneric<Integer>);
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}

{ TfrmMain }

procedure TfrmMain.FormCreate(Sender: TObject);
var ASG : TSomeGeneric<Integer>;
begin
ASG := TSomeGeneric<Integer>.Create();
with ASG do
try
//make use os the class ...
SomeProcedure(This); //error -> incompatible types
finally
Free();
end;
end;

procedure TfrmMain.SomeProcedure(ASG: TSomeGeneric<Integer>);
begin
//...
end;

end.


我有点怀疑,此时TSomeGeneric = class(Unit1.TSomeGeneric)TSomeGeneric被定义为另一个类,而不是引用,但是我无法确认

最佳答案

编译器很清楚。有问题的行是这一行:

  SomeProcedure(This);


错误是:

[dcc32 Error] E2010 Incompatible types: 'Unit2.TSomeGeneric' and 'Unit1.TSomeGeneric'

That's because in that method call:

  • This is of type Unit1.TSomeGeneric<System.Integer> and
  • SomeProcedure expects a parameter of type Unit2.TSomeGeneric<System.Integer>.

And those are not the same type because you explicitly made them different.

type
TSomeGeneric<T> = class(Unit1.TSomeGeneric<T>);


定义了一个新类型,现在您有两种不兼容的类型。

您只需要删除 Unit2。除了停止合理的代码编译外,它没有其他目的。

关于delphi - 这个不兼容的类型错误怎么了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17115400/

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