Delphi 对 MkDir 的不明确重载调用

标签 delphi delphi-7

当我尝试调用 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/

相关文章:

delphi - 是否可以读取未嵌入应用程序的外部资源文件?

delphi - 并行(或免费注册 COM)和 Delphi?

windows - OEMToCharW 返回错误字符

mysql - 在delphi 7上用数组检索多个记录值

multithreading - 在 TidHTTPServer.OnCommandGet 内创建对象(带有计时器)失败

java - Delphi 与 C++ Builder - 对于使用 Win32 的 Java 程序员来说,这是更好的选择

Delphi 2010-IDE 不断停止在 CPU 调试窗口

delphi - 如何使用 GDI+ 通过 Alpha channel 调整 PNG 图像的大小?

delphi - Delphi无法识别存在多个打印机

delphi - 使所有者绘制的 TPageControl 选项卡看起来更好,就像没有所有者绘制一样