2011-12-07 25 views
3

重載操作我有此單元:與陣列

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我錯了。

+2

您可能會想要在隱式例程中使用Low()和High(),而不是常量0和90。 –

+0

不,索引0是一個計數器,當我在數組中添加一個元素時,索引0中的值增加1。對我來說,對低()和高()值不重要。 –

回答

1

這裏是你正在嘗試做一個工作示例:

type 
    TIns = record 
    private type 
    TInsArray = array [0..90] of Integer; 
    private var 
    FInsArray : TInsArray; 
    function GetItem(index : integer) : integer; 
    function GetCount : integer; 
    public 
    class operator Implicit(const Value: Integer): TIns; 
    class operator Add(const Elem1: TIns; const Elem2: Integer): TIns; 
    property Items[index : integer] : integer read GetItem; default; 
    property Count : integer read GetCount; 
    end; 

function TIns.GetCount: integer; 
begin 
    Result := Self.FInsArray[0]; 
end; 

function TIns.GetItem(index: integer): integer; 
begin 
    Result := Self.FInsArray[index]; 
end; 

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 
    Result := Elem1; 
    Inc(Result.FInsArray[0]); 
    Result.FInsArray[Result.FInsArray[0]] := Elem2; 
end; 

var 
i : integer; 
s,s1 : TIns; 
begin 
    s := 0;  // Initialize ins; similar t := [] // 
    s1 := 0; 
    s := s + 5; // Add a element, in this case 5 // 
    s1 := s1 + 10; 
    for i := 1 to s.Count do 
     writeln('S[',i,']=',s[i]); // Read element values in s 
    for i := 1 to s1.Count do 
     writeln('S1[',i,']=',s1[i]); // Read element values in s1 
    ReadLn; 
end. 

拔出數組元素,聲明默認屬性物品。 通過屬性Count來公開元素數量。如Uwe指出的那樣,在Add操作符中首先將結果設置爲Elem1。

6

在TExtracts.Add中,您從不初始化包含Elem1內容的結果。這就是爲什麼你總是以空的結果結束。

更新:你的改變比做得更好!現在您正在寫入類方法中的記錄字段。這是不可能的,因爲錯誤信息正在試圖澄清。更不用說我不知道​​什麼MyVal應該是有益的。

+1

+1。很好的結果 - 我必須三次讀到它,然後看看你的答案,並看到你已經有了。 :) –

+0

我試圖解釋更好的問題。我忘了在我的程序中添加s:= s + 5。我希望如此更加正確。再次感謝。 –

+0

您的更改不能解決我答案中描述的錯誤。在進行更改之前,您仍然不會將Elem1的內容複製到結果中。記住:一個函數的返回值就像一個局部變量,除非你明確地把它放進去,否則不包含任何有效的值。 –