重載操作我有此單元:與陣列
unit Main.TIns;
interface
type
TIns = record
private type
TInsArray = array [0..90] of Integer;
var
FInsArray: TInsArray;
public
class operator Implicit(const Value: Integer): TIns;
class operator Add(const Elem1: TIns; const Elem2: Integer): TIns;
end;
implementation
class operator TIns.Implicit(const Value: Integer): TIns;
var
iIndex: Integer;
begin
if Value = 0 then
for iIndex := 0 to 90 do Result.FInsArray[iIndex] := 0;
end;
class operator TIns.Add(const Elem1: TIns; const Elem2: Integer): TIns;
begin
Inc(Result.FInsArray[0]);
Result.FInsArray[Result.FInsArray[0]] := Elem2;
end;
end.
而且主程序是:
program InsMain;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Main.TIns in 'Main.TIns.pas';
var
s: TIns;
begin
s := 0; // Initialize ins; similar t := [] //
s := s + 5; // Add a element, in this case 5 //
writeln(s[0]); // Read number of element in s, should to be 1 //
end.
的問題是,我收到此錯誤:[DCC錯誤] InsMain.dpr(20 ):E2149類沒有默認屬性。 爲什麼我不能讀取數組的元素? 我也鑫卡特解決添加變量MYVAL例如,這樣做:
type
TIns = record
private type
TInsArray = array [0..90] of Integer;
var
FInsArray: TInsArray;
public
MyVal: TInsArray;
class operator Implicit(const Value: Integer): TIns;
class operator Add(const Elem1: TIns; const Elem2: Integer): TIns;
end;
然後我修改添加這樣:
class operator TIns.Add(const Elem1: TIns; const Elem2: Integer): TIns;
begin
Inc(Result.FInsArray[0]);
Result.FInsArray[Result.FInsArray[0]] := Elem2;
MyVal := Result; // <--- error E2124 here.
end;
和寫作:
writeln(s.MyVal[0]);
不返回錯誤,但在添加,寫入時出錯:[DCC錯誤] Main.TIns.pas(31):E2124實例成員'MyVal'在這裏無法訪問,所以不能理解wh我錯了。
您可能會想要在隱式例程中使用Low()和High(),而不是常量0和90。 –
不,索引0是一個計數器,當我在數組中添加一個元素時,索引0中的值增加1。對我來說,對低()和高()值不重要。 –