德尔福:Char和TCharArray&QUOT阵列;不兼容的类型和QUOT; [英] Delphi: array of Char and TCharArray "Incompatible Types"

查看:115
本文介绍了德尔福:Char和TCharArray&QUOT阵列;不兼容的类型和QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到这个不兼容的类型错误以下几十倍的注释运行,并且从来没有高兴为什么这不,2007年德尔福直接支持:

I've run across this "Incompatible types" error in the comment below a few times, and never been happy with why this isn't directly supported in Delphi 2007:

program Project1; {$APPTYPE CONSOLE}

type TCharArray = array of Char;

procedure DoArray(Chars: array of Char);
begin
end;

function ReturnTCharArray: TCharArray;
var CharArray: TCharArray;
begin
  Result := CharArray;
end;

begin
  DoArray(ReturnTCharArray); // [DCC Error] Project1.dpr(18): E2010 Incompatible types: 'Array' and 'TCharArray'
end.

难道不应该有可能使一个数组类型的别名相互兼容的另一个数组类型?假设我不能改变DoArray(它是一个第三方库的一部分)的声明,我怎么写一个函数返回与DoArray的参数兼容的字符数组?这种简单的功能ReturnAChar:字符数组;结果在识别符预期,但数组发现的错误。我甚至试图改变功能的阵列返回与婴儿车一个VAR字符数组的过程,但也不允许设置字符数组参数的长度在程序(常量对象不能被传递作为变量参数)。

Shouldn't it be possible to make an array type "aliased" to another array type compatible with each other? Assuming I can't change the declaration of DoArray (it is part of a third party library), how do I write a function returning an array of char compatible with DoArray's param? The straightforward "function ReturnAChar: array of Char;" results in an "Identifier expected but 'ARRAY' found" error. I even tried changing the function returning the array to a procedure with a var "array of Char" pram, but that also doesn't allow setting the length of the "array of Char" param in the procedure ("Constant object cannot be passed as var parameter").

推荐答案

在<一个href=\"http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirstypecheckedpointers_xml.html\"相对=nofollow>输入@运算是关闭的编译器不会检查分配给一个指针什么,所以你可以调用一个程序的错误的参数:

When typed @ operator is off the compiler does not check what you assign to a pointer, so you can call a procedure with wrong parameters:

program Project1; {$APPTYPE CONSOLE}

type TCharArray = array of Char;

procedure DoArray(Chars: array of Char);
begin
end;

function ReturnTCharArray: TCharArray;
var CharArray: TCharArray;
begin
  Result := CharArray;
end;

type TFakeDoArray = procedure(Chars: TCharArray);

var
  FakeDoArray: TFakeDoArray;
begin
  FakeDoArray := @DoArray;
  FakeDoArray(ReturnTCharArray);
end.

虽然编译器不会抱怨,因为这个原因吉荣表示了他对<评论href=\"http://stackoverflow.com/questions/3780235/delphi-array-of-char-and-tchararray-incompatible-types/3780279#3780279\">Mason's回答,这是不行的。

然后,您可以尝试声明兼容的假程序使用一个与开放数组参数:

You can then try declaring your fake procedure compatible with one with an open array parameter:

program Project1; {$APPTYPE CONSOLE}

type TCharArray = array of Char;

procedure DoArray(Chars: array of Char);
begin
end;

function ReturnTCharArray: TCharArray;
var CharArray: TCharArray;
begin
  Result := CharArray;
end;

type
  TFakeDoArray = procedure(AnArray: Pointer; High: Integer);

var
  FakeDoArray: TFakeDoArray;
  Tmp: TCharArray;
begin
  FakeDoArray := @DoArray;
  Tmp := ReturnTCharArray;
  FakeDoArray(Tmp, High(Tmp));
end.

学分是他大文章截止鲁迪。和相关文件(从<一个href=\"http://docs.embarcadero.com/products/rad_studio/radstudio2007/RS2007_helpupdates/HUpdate4/EN/html/devcommon/programcontrolov_xml.html#50617373696E6720506172616D6574657273\"相对=nofollow>程序控制):

Credits are due Rudy for his great article. And the relevant documentation (from Program Control):

开放式阵列参数作为传递
  两个32位值。的第一个值是
  一个指针阵列数据,并且
  第二值比少一
  数组中元素的个数。

An open-array parameter is passed as two 32-bit values. The first value is a pointer to the array data, and the second value is one less than the number of elements in the array.

这篇关于德尔福:Char和TCharArray&QUOT阵列;不兼容的类型和QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆