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

查看:34
本文介绍了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.

难道不能让一个数组类型别名"为另一个相互兼容的数组类型吗?假设我不能更改 DoArray 的声明(它是第三方库的一部分),我该如何编写一个返回与 DoArray 的参数兼容的字符数组的函数?直截了当的函数 ReturnAChar:Char 数组;"导致需要标识符,但找到了 'ARRAY'"错误.我什至尝试将返回数组的函数更改为带有 varchar 数组"婴儿车的程序,但这也不允许在程序中设置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.

请参阅当参数类型是开放的 Char 数组时是否允许 Char 的动态数组?问题和可能的解决方法.

See Is a dynamic array of Char allowed when the parameter type is open array of Char? for a description of the problem and a possible work around.

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

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