Delphi:Char和TCharArray的数组“不兼容类型” [英] Delphi: array of Char and TCharArray "Incompatible Types"

查看:602
本文介绍了Delphi:Char和TCharArray的数组“不兼容类型”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的评论中遇到过这个不兼容类型的错误,并且从来没有为什么在Delphi 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.

不应该可以使数组类型aliased其他?假设我不能改变DoArray的声明(它是第三方库的一部分),我如何编写一个函数返回一个与DoArray参数兼容的char数组?直接的函数ReturnAChar:Char;导致标识符期望但是ARRAY'found错误。我甚至尝试更改函数返回数组到一个具有varcharpram的数组的过程,但是也不允许在过程中设置Char数组参数的长度(常量对象不能传递作为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").

推荐答案

这可能实际上是一个编译器错误(或从来没有正确记录的限制)。我做了一些实验,发现你可以传递一个动态数组(类型或不是)一个程序期望几乎每一种类型的开放数组...除了Char和WideChar。

This may actually be a compiler bug (or a limitation that was never documented properly). I did some experimentation and found that you can pass a dynamic array (typed or not) to a procedure expecting an open array for almost every type... except Char and WideChar.

有关问题的描述和可能的工作,请参见 http://stackoverflow.com/q/3781691/71200

See http://stackoverflow.com/q/3781691/71200 for a description of the problem and a possible work around.

这篇关于Delphi:Char和TCharArray的数组“不兼容类型”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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