加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – E2010不兼容的类型,为什么?

发布时间:2020-12-15 04:05:45 所属栏目:大数据 来源:网络整理
导读:我收到这个错误: [DCC Error] JwaStrSafe.pas(2277): E2010 Incompatible types: 'PSTRSAFE_LPWSTR' and 'PSTRSAFE_LPTSTR' 以下是来自JwaStrSafe.pas(来自Jedi Api)的代码的相关部分,我正在使用UNICODE定义的符号进行编译: typeSTRSAFE_LPWSTR = PWIDECHA
我收到这个错误:
[DCC Error] JwaStrSafe.pas(2277): E2010 Incompatible types: 'PSTRSAFE_LPWSTR' and 'PSTRSAFE_LPTSTR'

以下是来自JwaStrSafe.pas(来自Jedi Api)的代码的相关部分,我正在使用UNICODE定义的符号进行编译:

type

STRSAFE_LPWSTR = PWIDECHAR;
PSTRSAFE_LPWSTR = ^STRSAFE_LPWSTR;

{$IFDEF UNICODE}
  STRSAFE_LPTSTR = STRSAFE_LPWSTR;
  PSTRSAFE_LPTSTR = ^STRSAFE_LPTSTR;
{$ELSE}
  ...
{$ENDIF}

...
//function declaration
function StringCchCopyExW(
  {__out_ecount(cchDest)}pszDest : STRSAFE_LPWSTR;
  {__in}cchDest : size_t;
  {__in}const pszSrc : STRSAFE_LPCWSTR;
  {__deref_opt_out_ecount(pcchRemaining^)}ppszDestEnd : PSTRSAFE_LPWSTR;
  {__out_opt}pcchRemaining : PSize_t;
  {__in}dwFlags : Cardinal) : HRESULT; stdcall; forward; external;

...
//var passed to function
ppszDestEnd : PSTRSAFE_LPTSTR;

...

{$IFDEF UNICODE}
  result := StringCchCopyExW(pszDest,cchDest,pszSrc,ppszDestEnd,pcchRemaining,dwFlags);
{$ELSE}
  result := StringCchCopyExA(pszDest,dwFlags);
{$ENDIF}

我在参数ppszDestEnd上调用StringCchCopyExW时出错.

查看类型定义我明白PSTRSAFE_LPTSTR是STRSAFE_LPTSTR的指针类型,它只是STRSAFE_LPWSTR的别名,为什么PSTRSAFE_LPTSTR和PSTRSAFE_LPWSTR不兼容?


感谢大卫的解释我取代了

PSTRSAFE_LPTSTR = ^STRSAFE_LPTSTR;

PSTRSAFE_LPTSTR = PSTRSAFE_LPWSTR;

现在代码编译没有错误.

谢谢

解决方法

我可以在XE2中轻松地重现这一点,我想它在所有其他版本中的行为都是一样的.为了简单起见,我将其简化为:
program PointerTypeCompatibility;
{$APPTYPE CONSOLE}
type
  A = Integer;
  B = Integer;
var
  ptA: ^A;
  ptB: ^B;
begin
  ptA := ptB;
end.

这也产生了E2010.但是,如果启用type-checked pointers选项,则代码将成功编译.实际上,该编译器选项的文档指出:

In the {$T-} state,distinct pointer types other than Pointer are incompatible (even if they are pointers to the same type). In the {$T+} state,pointers to the same type are compatible.

感谢Ken White为我指出了有用的帮助主题Type Compatibility and Identity.相关的摘录是T1和T2类型在以下情况下是分配兼容的:

T1 and T2 are compatible pointer types.

文档还指出类型是类型兼容的,如果:

Both types are (typed) pointers to the same type and the {$T+} compiler directive is in effect.

所以这记录了观察到的行为并引导我到这个例子:

program PointerTypeCompatibilityTake2;
{$APPTYPE CONSOLE}
{$TYPEDADDRESS OFF}
var
  P1,P2: ^Integer;
  P3: ^Integer;
begin
  P1 := P2;//compiles
  P1 := P3;//E2008 Incompatible types
end.

所以,总结一下:

>当禁用类型检查指针时,如果指针属于同一类型,则指针是赋值兼容的.
>当启用类型检查指针时,如果指针指向相同类型,则指针是赋值兼容的.

我不得不承认对类型检查指针设置背后的历史和推理一无所知,所以我无法解释为什么编译器就是这样的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读