我有一個非?;竞秃唵蔚念悾缦滤荆簡卧b載機;interfaceuses Vcl.Dialogs;type TLoader = Class(TObject) published constructor Create(); end;implementation{ TLoader } constructor TLoader.Create;begin ShowMessage('ok');end;end.從Form1中我這樣稱呼它:procedure TForm1.Button1Click(Sender: TObject);var the : TLoader;begin the := the.Create;end;現在,在the := the.Create零件之后,delphi會顯示消息,'ok'然后給我一個錯誤,然后說Project Project1.exe raised exception class $C0000005 with message 'access violation at 0x0040559d: read of address 0xffffffe4'.它還顯示此行:constructor TLoader.Create;begin ShowMessage('ok');end; // <-------- THIS LINE IS MARKED AFTER THE ERROR.我是delphi的新手。我正在使用Delphi XE2,但無法解決此錯誤。有人給我顯示路徑或對此有解決方案嗎?
3 回答

當年話下
TA貢獻1890條經驗 獲得超9個贊
var
the : TLoader;
begin
the := the.Create;
是不正確的。它應該是
var
the : TLoader;
begin
the := TLoader.Create;

小唯快跑啊
TA貢獻1863條經驗 獲得超2個贊
您的語法有誤。如果要構造一個新對象,則應在構造函數調用中使用類名而不是變量名:
procedure TForm1.Button1Click(Sender: TObject);
var
the : TLoader;
begin
the := TLoader.Create;
end;

皈依舞
TA貢獻1851條經驗 獲得超3個贊
不,您不能這樣做,@ Arioch。該ClassType
函數通過從給定對象中讀取類引用來工作。在您的建議中,ClassType
將從一個未初始化的變量中讀取數據,因為還沒有有效的對象引用,這與原始問題相同。ClassType
是一個實例方法,因此您需要一個實例。
添加回答
舉報
0/150
提交
取消