[英]Delphi incompatible types
我有一個非常奇怪的錯誤,我嘗試重新啟動我的IDE,但是並沒有解決它。
我創建了一個界面,如下所示:
myInterface = interface
['{delphi guid key here}'] (CTRL+ALT+G)
function getDataPCE : IDataPCE;
property dataPCE : IDataPCE read getDataPCE;
(some other properties that works)
end;
然后我創建了從該接口繼承的對象
myObject = class(TInterfacedObject, myInterface)
private
...
function getDataPCE : IDataPCE;
...
public
...
property dataPCE : IDataPCE read getDataPCE;
...
end;
“ ...”表示還有一些其他屬性和功能,但與此無關。
我收到此錯誤:“不兼容的類型”
我該如何解決?
編輯
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;
為了幫助您了解如何提出問題,以下是您應提交的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.
這導致此錯誤:
[dcc32錯誤] E2008不兼容的類型
原因是類型IRioPiece
的屬性的屬性IRioPiece
必須是不接受任何參數且返回類型為IRioPiece
。 但是您的getter函數需要兩個參數,它們需要來自某個地方。 如上所述,訪問屬性時不提供這些參數。
因此,您可以通過將getDataPCE
的聲明更改為getDataPCE
來修復編譯錯誤:
function getDataPCE: IRioPiece;
但這幾乎肯定是錯誤的解決方案。 大概您getDataPCE
這些參數聲明為getDataPCE
因為您需要提供它們。 在這種情況下,您將無法刪除它們。 這意味着你不能聲明一個簡單的屬性dataPCE
由支持getDataPCE
。 我的猜測是,您只需要刪除dataPCE
屬性。
當然,您可以這樣聲明一個數組屬性 :
property dataPCE[numRegister: String; numPCEFormated: String]: IRioPiece
read getDataPCE;
這意味着您將按以下方式訪問屬性:
dataPCE := resvervation.dataPCE[numRegister, numPCEFormatted];
但是對我來說,這超出了財產的使用范圍。 我認為最好使用一個函數來訪問它。
結論
刪除dataPCE
屬性,並讓接口的getDataPCE
代替。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.