怎样使用另一个单元中的函数?

hotel9545 2003-10-16 01:50:51
我想把所有的自定义函数放在一个单元文件中然后

在别的单元中

uses 自定义函数单元;

直接使用自定义函数;

例:
————————————————————————
unit Unit2;

interface

implementation

function max(x,y:integer):integer;
var
tmp:integer;
begin
tmp:=x;
if y> x then tmp:=y;
Result:=tmp;
end;
end.

————————————————————
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses
Unit2;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption:=IntToStr(max(12,16));
end;

end.

但总是:
[Error] Unit1.pas(30): Undeclared identifier: 'max'
...全文
37 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwyang80 2003-10-16
  • 打赏
  • 举报
回复
调试通过,给分啊!
hwyang80 2003-10-16
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,unit2;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption:=IntToStr(form2.max(12,16));
end;

end.

unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm2 = class(TForm)
private
{ Private declarations }
public
function max(x,y:integer):integer;
end;

var
Form2: TForm2;

implementation

{$R *.dfm}
function tform2.max(x,y:integer):integer;
var
tmp:integer;
begin
tmp:=x;
if y> x then tmp:=y;
Result:=tmp;
end;
end.

end.
angle097113 2003-10-16
  • 打赏
  • 举报
回复
在单元中alt+F11选中要引用的单元
单元名称.函数的名称 就可以了
lead001 2003-10-16
  • 打赏
  • 举报
回复
将单元2加到interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,单元2;
vcshcn 2003-10-16
  • 打赏
  • 举报
回复
unit Unit2;

interface
function max(x,y:integer):integer;

implementation

function max(x,y:integer):integer;
var
tmp:integer;
begin
tmp:=x;
if y> x then tmp:=y;
Result:=tmp;
end;
end.

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧