-1
我試圖在我的程序中使用類。編輯類中的成員變量時出現錯誤
TStack = Class
Public
constructor Create; Overload;
Procedure Add(Frm:TForm);
Procedure Remove();
Procedure Do_Close;
Private
List : Array[1..Max_Forms] of Rec;
Count : Byte;
End;
構造:
constructor TStack.Create;
begin
Self.Count := 0;
end;
Procedure TStack.Add(Frm:TForm);
begin
Inc(Self.Count);
List[Count].Form := @Frm;
List[Count].Width := Frm.Width;
List[Count].Height := Frm.Height;
List[Count].left := Frm.Left;
List[Count].Top := Frm.Top;
end;
我不能改變計數變量的值!它會導致運行時錯誤:訪問衝突....寫入地址000001E4
什麼問題?
有關詳情:
我想要一個指針給每個窗體保存在這樣的結構:
Rec = Record
Form : ^TForm;
Maximized : Boolean;
Width,
Height,
left,
Top : Integer;
End;
然後
Procedure TStack.Do_Close;
var
i : integer;
MyForm : TForm;
begin
i := .....some code here.......;
MyForm := @List[i].Form;
ShowMessage('I will close '+MyForm.Caption);
MyForm.Close;
end;
,並調用構造函數這樣初始化「計數」:
Stack.Create;
哪一行崩潰了,在構造函數中還是在Add中? – Blorgbeard
很多問題。可能傷害你的那個是你沒有正確創建對象。你沒有顯示該代碼。請做。使用整數來計數,而不是字節。不要使用靜態數組。使用動態數組或'TList'。從0開始索引數組。不要取本地變量的地址。 –
是的,'Stack.create'不工作,但'stack = Tstack.create'解決了一些問題(爲什麼?!)!爲什麼我不應該使用靜態數組?你是什麼意思關於「不要接受局部變量的地址」!謝謝你們:) – Jessica