delphi - delphi中另一个单元的过程显示未声明的标识符

标签 delphi

我似乎无法将另一个单元的程序链接到以主单元的形式工作。我尝试在接口(interface)下方添加过程声明,如本问题 How to run procedure from another unit? 中所述,但它没有用。一直显示 [DCC 错误] Main.pas(27): E2003 未声明的标识符:'sayHi' 以下是两个单位的代码:
Main.pas:

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Unit2;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
sayHi();
end;

end.

和 Unit2.pas
unit Unit2;

interface uses Dialogs;

procedure sayHi();

implementation

procedure sayHi();
begin
  ShowMessage('hi');
end;

end.

这是该项目的 dpr 文件:
program gl;

uses
  Vcl.Forms,
  Main in 'Main.pas' {Form1},
  Unit2 in 'Unit2.pas';

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

这是 main.dfm 文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 444
  ClientWidth = 621
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
end

最佳答案

我以前见过这个,它总是与首先找到不同版本的“Unit2”有关。

您的机器上有多个 Unit2.dcu 或 pas。
首先找到没有“SayHi”的Unit2。

请检查您的项目和 Delphi Global 库路径。

关于delphi - delphi中另一个单元的过程显示未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22897414/

相关文章:

delphi - 使用省略号缩短 Firemonkey 中的标签

delphi - Delphi中.Create和.Create()有什么区别?

delphi - 如何从非模式窗体显示模式对话框?

delphi - 如何绘制文本居中的纯色位图?

delphi - 我应该将 FastMM 包含文件包含在我的 Delphi 6 应用程序安装文件中吗?

delphi - delphi XE中如何读取文件内容到字符串

delphi - 从 inno setup 获取 exe 或 dll 的版本

Delphi在页面控件中按标签号显示框架

delphi - 将 Delphi 窗体最小化到系统托盘

delphi - 调用外部函数给出不满足的前向或外部声明