[英]Delphi incompatible types
I have a really strange bug, I tried to restart my IDE but it didn't fixed it. 我有一个非常奇怪的错误,我尝试重新启动我的IDE,但是并没有解决它。
I've created a interface that looks like this: 我创建了一个界面,如下所示:
myInterface = interface
['{delphi guid key here}'] (CTRL+ALT+G)
function getDataPCE : IDataPCE;
property dataPCE : IDataPCE read getDataPCE;
(some other properties that works)
end;
Then i created my object that inherits from this interface of course 然后我创建了从该接口继承的对象
myObject = class(TInterfacedObject, myInterface)
private
...
function getDataPCE : IDataPCE;
...
public
...
property dataPCE : IDataPCE read getDataPCE;
...
end;
the "..." means there some other properties and functions but not related to this. “ ...”表示还有一些其他属性和功能,但与此无关。
And I'm getting this error: "Incompatible types" 我收到此错误:“不兼容的类型”
How can I solve this? 我该如何解决?
EDIT 编辑
IInfoNotisReservation = interface
['{AE5CEC31-B2CE-4A3D-9CFE-6393646E4A04}']
function getNumberPCE : String;
function getDataPCE(numRegister : String; numPCEFormated : String): IRioPiece;
procedure setNumberPCE(NumberPCE: String);
function getRegName : String;
procedure setRegName(RegName: String);
function getRegKey : String;
procedure setRegKey(RegKey: String);
property NumberPCE : String read getNumberPCE write setNumberPCE;
property RegName : String read getRegName write setRegName;
property RegKey : String read getRegKey write setRegKey;
property DataPCE : IRioPiece read getDataPCE;
end;
type
TInfoNotisReservation = class(TInterfacedObject, IInfoNotisReservation)
private
DataBase : IDataBase;
SuperRio : ISuperRio;
RioN : IRio;
fPCENum : String;
function getDataPCE(numRegister : String; numPCEFormated : String): IRioPiece;
function getNumberPCE: string;
function getRegKey: string;
function getRegName: string;
procedure setNumberPCE(NumberPCE: string);
procedure setRegKey(RegKey: string);
procedure setRegName(RegName: string);
procedure setRioN(Registre: string);
public
Constructor Create;
property DataPCE : IRioPiece read getDataPCE;
property NumberPCE : String read getNumberPCE write setNumberPCE;
property RegName : String read getRegName write setRegName;
property RegKey : String read getRegKey write setRegKey;
end;
function TInfoNotisReservation.getDataPCE(numRegister,
numPCEFormated: String): IRioPiece;
begin
setRioN(numRegister);
Result := RioN.GetPieceByID(RioN.PieceNumberToID(NumPCEFormated).Item[0].ID, FLAG_IGNORE_SECURITY);
end;
For the sake of helping you understand how to ask a question, here is the MCVE that you should have submitted. 为了帮助您了解如何提出问题,以下是您应提交的MCVE。
type
IRioPiece = interface
end;
IInfoNotisReservation = interface
['{AE5CEC31-B2CE-4A3D-9CFE-6393646E4A04}']
function getDataPCE(numRegister: String; numPCEFormated: String): IRioPiece;
property dataPCE: IRioPiece read getDataPCE; // ERROR HERE
end;
begin
end.
This results in this error: 这导致此错误:
[dcc32 Error] E2008 Incompatible types
[dcc32错误] E2008不兼容的类型
The reason is that a property getter for a property of type IRioPiece
must be a function that accepts no parameters and has return type of IRioPiece
. 原因是类型
IRioPiece
的属性的属性IRioPiece
必须是不接受任何参数且返回类型为IRioPiece
。 But your getter function requires two arguments, and they need to come from somewhere. 但是您的getter函数需要两个参数,它们需要来自某个地方。 As written above, these arguments are not supplied when you access the property.
如上所述,访问属性时不提供这些参数。
So you could fix the compilation error by changing the declaration of getDataPCE
to: 因此,您可以通过将
getDataPCE
的声明更改为getDataPCE
来修复编译错误:
function getDataPCE: IRioPiece;
But that's almost certainly the wrong solution. 但这几乎肯定是错误的解决方案。 Presumably you declared those parameters to
getDataPCE
because you need to supply them. 大概您
getDataPCE
这些参数声明为getDataPCE
因为您需要提供它们。 In which case you cannot remove them. 在这种情况下,您将无法删除它们。 Which means that you cannot declare a simple property
dataPCE
that is backed by getDataPCE
. 这意味着你不能声明一个简单的属性
dataPCE
由支持getDataPCE
。 My guess is that you simply need to remove the dataPCE
property. 我的猜测是,您只需要删除
dataPCE
属性。
Of course, you could declare an array property like this: 当然,您可以这样声明一个数组属性 :
property dataPCE[numRegister: String; numPCEFormated: String]: IRioPiece
read getDataPCE;
Which would mean you access the property like this: 这意味着您将按以下方式访问属性:
dataPCE := resvervation.dataPCE[numRegister, numPCEFormatted];
But to me that is stretching the use of a property too far. 但是对我来说,这超出了财产的使用范围。 I think it is better to access this using a function.
我认为最好使用一个函数来访问它。
Conclusion 结论
Remove the dataPCE
property and have consumers of the interface call getDataPCE
instead. 删除
dataPCE
属性,并让接口的getDataPCE
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.