我在一个类的外面定义了一个record
type
  nerveCell = record
    ....
    weight: array of double; //动态数组
    ....
 end;TInputLevel = array[0..inputLevelNo - 1] of nerveCell;然后定义了一个类:
TBP = class(TObject)
  private
    FInputLevel: TinputLevel; //定义一个数组,数组元素是nerveCell   public
    function  getInputLevelElementWeight(I: integer): double;
    property  InputLevelElementWeight[I: integer]: double read
                    getInputLevelElementWeight write setInputLevelElementWeight;
  end;//end class definiation
我想写一个getInputLevelElementWeight函数,返回record里面的weight数组,可是这样的函数却编译不过:
function  TBP.getInputLevelElementWeight(I: integer): double;
begin
  if (I >= 0) and (I < Length(FInputLevel)) then
    result := FInputLevel[I].weight;
end;
应该怎样写呢?

解决方案 »

  1.   

    result := FInputLevel[I].weight;
    result是double型,而FInputLevel[I].weight是一个数组型,类型不匹配啊。
      

  2.   

    解决的办法不少,比如返回一个指针,或者参数回调。
    procedureTBP.getInputLevelElementWeight(I: integer;var aWeight: array of double);
    begin
      if (I >= 0) and (I < Length(FInputLevel)) then
    begin
      setlength();//给aWeight 设定长度
      aWeight := FInputLevel[I].weight;
    end;
    end;
      

  3.   

    首先是类型不匹配,你应该这样返回:
       result := FInputLevel[I].weight[j];
      j是你定义的另一个整型变量,来返回WEIGHT数组的元素。
      

  4.   

    解决的办法不少,比如返回一个指针,或者参数回调。
    -----------------------------------------------
    写成“返回一个指针”的形式该怎么样写呢?我想把这个函数用在
     property  InputLevelElementWeight[I: integer]: double read getInputLevelElementWeight 
    中。
    如果采用参数回调,那么这个函数可以改成过程的形式,写在read后面吗?
      

  5.   


    参数回调:
    procedure  TBP.getInputLevelElementWeight(I: integer; var aWeight: array of double);
    begin
      if (I >= 0) and (I < Length(FInputLevel)) then
        aWeight := FInputLevel[I].weight; //出错的地方
    end;
    出现:Incompatible types: 'Array' and 'dynamic array'
    错误,怎么样改正?
      

  6.   

    怎么样定义一个动态整型数组aWeight,然后把这个数组作为一个procedure的形参?这个procedure的声明的一般形式应该是怎么样的?
      

  7.   

    目前的第一个问题是:
    把 InputLevelElementWeight属性定义成一个动态double型数组那样子的属性,该怎么定义?