首页 > 解决方案 > Delphi:左侧无法分配给

问题描述

我正在将一些 C 代码转换为 Delphi。C 代码具有从 2D (x,y) 索引转换为 1D 索引的读/写功能。

当我尝试在 Delphi 中做同样的事情时,出现“无法将左侧分配给”错误。

这是我可以创建的最简单的代码来显示错误。对不起,长度。可以使用单个按钮将其完整复制/粘贴到新表单中以显示问题。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type ClassA=class(TObject)
     private
            type DoubleArray=array of double;
            var _src:DoubleArray;
            _w,_h:integer;
     public
           constructor Create(w,h:integer);
           destructor Destroy; reintroduce;
           function at(x,y:integer):double;
end;

type ClassB=class(TObject)
     private
            type DoubleArray=array of double;
            var _d:ClassA;
            //width and height
            _w,_h:integer;
     public
           constructor Create(w,h:integer);
           destructor Destroy; reintroduce;
           procedure CauseError;
end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor ClassA.Create(w,h:integer);
begin
     _w:=w;
     _h:=h;
     setlength(_src,_w*_h);
end;

destructor ClassA.Destroy;
begin
     setlength(_src,0);
end;

function ClassA.at(x,y:integer):double;
begin
     at:=_src[x+y*_w];
end;

constructor ClassB.create(w,h:integer);
begin
     _w:=w;
     _h:=h;
     _d:=ClassA.create(_w,_h);
end;

destructor ClassB.Destroy;
begin
     _d.free;
end;

procedure ClassB.CauseError;
begin
     _d.at(10,10):=_d.at(10,10)+1;
end;


procedure TForm1.Button1Click(Sender: TObject);
var CB:ClassB;
begin
     CB:=ClassB.create(100,100);

     CB.free;
end;

end.

谁能帮助我需要使用什么语法才能使“at”函数起作用?

我没有在课堂上做太多工作,所以在这里遇到了麻烦。

编译器在这一行抱怨

_d.at(10,10):=_d.at(10,10)+1;

好的,这是C代码。它有读写版本。我如何让 Delphi 做到这一点?

C函数如下

     /* Read-only and read-write access to grid cells */
    double at(int x, int y) const {
        return _src[x + y*_w];
    }

    double &at(int x, int y) {
        return _src[x + y*_w];
}

标签: delphi

解决方案


推荐阅读