*****************************************************************
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
    bbb: Taaa;
    constructor Create;
    destructor Destroy; override;
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}constructor TForm1.Create;
begin
  bbb := Taaa.Create();
end;destructor TForm1.Destroy;
begin
  inherited Destroy;
  bbb.Free;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  bbb.Fbbb := 'aaaa';
end;end.
*****************************************************************
unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  Taaa = class(TObject)
  private
  public
    Fbbb: string;
  end;implementationend.
unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  Taaa = class(TObject)
  private
  public
    Fbbb: string;
  end;implementationend.
unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  Taaa = class(TObject)
  private
  public
    Fbbb: string;
  end;implementationend.
*****************************************************************代码可以正常编译,但是在运行的时候会报错,为什么呀,怎么解决???

解决方案 »

  1.   

    拜托,你为什么要给TForm1画蛇添足呀,type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private    { Private declarations }
      public
        bbb: Taaa;
        constructor Create;              // 把这两行
        destructor Destroy; override;    // 去掉
        { Public declarations }
      end;
    还有destructor TForm1.Destroy;
    begin
      inherited Destroy;          // 把这行去掉
      bbb.Free;
    end;
    OK。
      

  2.   

    我是想在form1中读或者写另外一个自定义class的字段,该这么做,我上面做的程序会报错
      

  3.   

    楼主按我上面写的做就可以了,在form1中读或者写另外一个自定义class的字段,你做到了,不是么?
      

  4.   

    这两行倒过来试试看  inherited Destroy;          // 把这行去掉
      bbb.Free;
    ==>
      bbb.Free;
      inherited Destroy;          // 把这行去掉
      

  5.   

    有是窗体释放问题
    这里是最容易出错误的地方
    你改一下这里
    按楼上说的,交换一下位置看看如下:
    在Unit1中destructor TForm1.Destroy;
    begin
      bbb.Free;//已经交换了顺序,看看可以吗?
      inherited Destroy;
    end;