使用Delphi,为何包装类型数组引发需记录、对象或类类型错误?

更新于
2026-10-03 11:14:06
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计455个文字,预计阅读时间需要2分钟。

我有两种arapper类型,便于处理/返回一维数组。我想写一个方法,将一个转换为一个(2d-float Vector类转换为2d-int Point类)。我已经写了一个简单的版本,但它只抛出一些我不明白的错误。

javaclass UUtil { // ...}

interface U { // ...}

public void convertVectorToPoint(U u) { // 简单的实现,但存在错误}

我有两个arapper类型,便于处理/返回一维数组,我想写一个方法将一个转换为另一个(2d-float-Vector类到2d-int-point类).写了一个简单的,但它只是抛出一些我不明白的错误.

unit UUtil; interface uses UVector2f, Types, SysUtils; type Vector2fArrayWrapper = array of Vector2f; PointArrayWrapper = array of TPoint; implementation function toPointArray(vw : Vector2fArrayWrapper) : PointArrayWrapper; var pw : PointArrayWrapper; i,x,y : Integer; begin setLength(pw, vw.length); for i := 0 to vw.high do begin x := round(vw[i].getX()); y := round(vw[i].getY()); vw[i] := TPoint(x,y); end; result := pw; end; end.

这些是我得到的错误:

[Error] UUtil.pas(20): Record, object or class type required [Error] UUtil.pas(21): Record, object or class type required [Error] UUtil.pas(25): ')' expected but ',' found [Error] UUtil.pas(27): Declaration expected but identifier 'result' found [Error] UUtil.pas(28): '.' expected but ';' found 动态数组不是对象,类或记录.他们没有定义方法.

代替

vw.length

你必须写

Length(vw)

同样高.

接下来,TPoint是一种类型.如果你想创建一个新的,你使用辅助函数Point().

然后你分配给vw [i],但你的意思是指向pw [i].

最后,没有必要引入局部变量,然后为结果分配局部变量.您可以直接在Result上完成所有工作.所以,我写这样的代码:

function toPointArray(const vw: Vector2fArrayWrapper): PointArrayWrapper; var i: Integer; begin setLength( Result, Length(vw)); for i := 0 to high(vw) do Result[i] := Point(round(vw[i].getX), round(vw[i].getY)); end;

本文共计455个文字,预计阅读时间需要2分钟。

我有两种arapper类型,便于处理/返回一维数组。我想写一个方法,将一个转换为一个(2d-float Vector类转换为2d-int Point类)。我已经写了一个简单的版本,但它只抛出一些我不明白的错误。

javaclass UUtil { // ...}

interface U { // ...}

public void convertVectorToPoint(U u) { // 简单的实现,但存在错误}

我有两个arapper类型,便于处理/返回一维数组,我想写一个方法将一个转换为另一个(2d-float-Vector类到2d-int-point类).写了一个简单的,但它只是抛出一些我不明白的错误.

unit UUtil; interface uses UVector2f, Types, SysUtils; type Vector2fArrayWrapper = array of Vector2f; PointArrayWrapper = array of TPoint; implementation function toPointArray(vw : Vector2fArrayWrapper) : PointArrayWrapper; var pw : PointArrayWrapper; i,x,y : Integer; begin setLength(pw, vw.length); for i := 0 to vw.high do begin x := round(vw[i].getX()); y := round(vw[i].getY()); vw[i] := TPoint(x,y); end; result := pw; end; end.

这些是我得到的错误:

[Error] UUtil.pas(20): Record, object or class type required [Error] UUtil.pas(21): Record, object or class type required [Error] UUtil.pas(25): ')' expected but ',' found [Error] UUtil.pas(27): Declaration expected but identifier 'result' found [Error] UUtil.pas(28): '.' expected but ';' found 动态数组不是对象,类或记录.他们没有定义方法.

代替

vw.length

你必须写

Length(vw)

同样高.

接下来,TPoint是一种类型.如果你想创建一个新的,你使用辅助函数Point().

然后你分配给vw [i],但你的意思是指向pw [i].

最后,没有必要引入局部变量,然后为结果分配局部变量.您可以直接在Result上完成所有工作.所以,我写这样的代码:

function toPointArray(const vw: Vector2fArrayWrapper): PointArrayWrapper; var i: Integer; begin setLength( Result, Length(vw)); for i := 0 to high(vw) do Result[i] := Point(round(vw[i].getX), round(vw[i].getY)); end;