有两个单元,unit1,unit2
unit1引用unit2
为什么当点击unit1的button1时候出错?谢谢,,
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Unit2;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
   
  end;var
  Form1: TForm1;
  aaa:Taaa;   //我想把aaa定义为全局的,全局需要用到。
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  aaa.Create;  //运行到这出错。。
  aaa.show;
end;end.unit Unit2;interface  uses Qdialogs;
type  Taaa = class
  private  protected  public
      procedure show;
  published  end;
implementation{ Tplane }procedure Taaa.show;
begin
  showmessage('aaa');
end;end.

解决方案 »

  1.   


    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
      aaa:Taaa;   //我想把aaa定义为全局的,全局需要用到。//注意改动即可
       
      end;var
      Form1: TForm1;
    implementation
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      aaa.Create;  //运行到这出错。。
      aaa.show;
    end;
    aaa必须
    aaa := taaa.Create;
    aaa.Show;
    aaa.Free  //用完Free掉
    aaa.Create是直接调用Create并没有分配内存给aaa,所以出错
      

  3.   

    你Taaa的创建方式错了嘛,应该是:
      aaa := Taaa.Create;  
      aaa.show;