gpt4 book ai didi

Delphi 对 MkDir 的不明确重载调用

转载 作者:行者123 更新时间:2023-12-03 15:49:56 25 4
gpt4 key购买 nike

当我尝试调用 MkDir 时收到以下错误消息

[Error] DBaseReindexer.dpr(22): Ambiguous overloaded call to MkDir

我尝试了以下操作,但它们都返回相同的错误。

MkDir('test');

var
Dir: String;
begin
Dir := 'test';
MkDir(Dir);
end;

const
Dir = 'test';
begin
MkDir(Dir);
end;

从源代码来看,有一个版本采用字符串,另一个版本采用 PChar。我不确定我的字符串在这两种类型之间是否会产生歧义。

<小时/>

重现错误的代码(来自注释):

program Project1; 
{$APPTYPE CONSOLE}
uses SysUtils, System;
begin
MkDir('Test');
end.

最佳答案

您的代码在空项目中可以正常编译:

program Project1;

procedure Test;
const
ConstStr = 'test';
var
VarStr: string;
begin
MkDir('Test');
MkDir(ConstStr);
MkDir(VarStr);
end;

begin
end.

所以你的问题是你在代码的其他地方为 MkDir 定义了不兼容的重载。例如这个程序:

program Project1;

procedure MkDir(const S: string); overload;
begin
end;

procedure Test;
const
ConstStr = 'test';
var
VarStr: string;
begin
MkDir('Test');
MkDir(ConstStr);
MkDir(VarStr);
end;

begin
end.

产生以下编译器错误:

[dcc32 Error] Project1.dpr(13): E2251 Ambiguous overloaded call to 'MkDir'  System.pas(5512): Related method: procedure MkDir(const string);  Project1.dpr(3): Related method: procedure MkDir(const string);[dcc32 Error] Project1.dpr(14): E2251 Ambiguous overloaded call to 'MkDir'  System.pas(5512): Related method: procedure MkDir(const string);  Project1.dpr(3): Related method: procedure MkDir(const string);[dcc32 Error] Project1.dpr(15): E2251 Ambiguous overloaded call to 'MkDir'  System.pas(5512): Related method: procedure MkDir(const string);  Project1.dpr(3): Related method: procedure MkDir(const string);

Notice how the compiler helpfully tells you which two methods cannot be disambiguated. If you read the full compiler error message then it will take you to the cause of your problem.

Older Delphi versions don't give you the extra information. So if you are in that position, you will have to search your source code for the extra MkDir.

Update

Following the edit to the question that adds code, we can see that the incompatible overload arises from a rather surprising source. Your code is:

program Project1; 
{$APPTYPE CONSOLE}
uses SysUtils, System;
begin
MkDir('Test');
end.

好吧,System 会自动包含在每个单元中,编译器跳过 uses 子句是一个编译器缺陷。但错误地第二次包含 System 才是导致歧义的原因。

现代版本的 Delphi 解决了这个问题,您的代码结果为

[dcc32 Error] E2004 Identifier redeclared: 'System'

显然,解决方案是删除 System 的虚假使用。

关于Delphi 对 MkDir 的不明确重载调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com