小弟要做一幻方的作业,自己定义了一个类,里面是一个动态数组. 可是在分配空间的时候出错.  下面是代码
  //幻方类
  TMagicSquare = class
    private   //data
        Matrix: array of array of integer;
        Rows: integer;
    private   //module
        procedure Reset(RowCount:integer);
        procedure Clear;
    public
        function BuildMagicSquare(Rows:integer):integer;
  end;   // Class TMagicSquareprocedure TMagicSquare.Reset(RowCount:integer);
begin
    if Matrix<>nil then    **对象第一次使用时为什么Matrix<>nil?????
    begin
        Rows:=0;
        finaLize(Matrix);  **出错!!
    end;
    Rows:=RowCount;
    SetLength(Matrix,RowCount,RowCount);   **出错!!!(if语句注释掉以后)
end;

解决方案 »

  1.   

    不能用finaLize释放动态数组。动态数组是自动管理的,你可以不考虑它的释放。如果你需要人工释放,用:
      Matrix := nil;
    ————————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    ————————————————————————————————————
      

  2.   

    下面这样还是异常!!!! 我的是delphi6
    procedure TMagicSquare.Reset(RowCount:integer);
    begin
        Rows:=RowCount;
        SetLength(Matrix,RowCount,RowCount);   **出错!!!(if语句注释掉以后)
    end;
      

  3.   

    SetLength 分配以后如果空间不够还可以用SetLength分配 这是动态数组的长处 且里面的数据没有影响操作 lxpbuaa(桂枝香在故国晚秋)  说得没错