delphi - 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];
}
解决方案
推荐阅读
- html - CSS 中指定的字体在 Chrome 中有效,但在 Safari 中无效 - 为什么?
- javascript - 使用 Fetch(不是 NPM)删除 Stripe 订阅项目
- javascript - 使用 Jest 配置酶到 json
- c# - Xamarin 应用程序在具有动态参数返回的函数处崩溃
- java - spring-boot-configuration-processor 依赖仅用于 IDE 支持吗?
- aspnetboilerplate - PublishAsync 发布多个通知
- javascript - 如何在不改变纸张大小的情况下打印 div 的内容
- c++ - 对仅由值使用的静态 constexpr 成员的未定义引用
- android - 如何从 XML 文件中选择特定元素?
- bash - 如何在 bash 脚本中将 STDOUT 发送到一个文件并将 STDOUT+STDERR 发送到另一个文件?