unsatisfied foward or external declaration :'TForm.Divisor'(10分)

  • 主题发起人 主题发起人 tea_nine
  • 开始时间 开始时间
T

tea_nine

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下:RUN时提示unsatisfied foward or external declaration :'TForm.Divisor'
请问是哪里出错?

unit Unit1;

interface

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

type
TForm1 = class(TForm)
lblHint: TLabel;
edtNum1: TEdit;
edtNum2: TEdit;
lblRes: TLabel;
btnDivisor: TButton;
btnQuit: TButton;
function Divisor(x,y:integer):integer;
procedure btnQuitClick(Sender: TObject);
procedure btnDivisorClick(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function Divisor(x,y:integer):integer;
var r:integer;
begin
r:=x mod y;
while r<>0 do
begin
x:=y;
y:=r;
r:=x mod y;
end;
Divisor:=y;
end;

procedure TForm1.btnQuitClick(Sender: TObject);
begin
close
end;

procedure TForm1.btnDivisorClick(Sender: TObject);
var m,n,g:integer;
begin
m:=strtoint(edtNum1.text);
n:=strtoint(edtNum2.text);
g:=divisor(m,n);
lblRes.caption:='最大公约数为:'+inttostr(g);
end;

end.
 
是不是不用在TYPE里声明函数?
 
你在interface部分将function Divisor声明在了TForm1的类定义之内,那么Divisor就与
下面的btnQuitClick一样是TForm1的成员方法,就应当和btnQuitClick一样在实现部分加上
TForm1.前缀。考虑到你的Divisor函数代码与TForm1并没有任何联系,那就应当将前面的声
明从TForm1内移出来,声明为一个普通函数而不是类函数——放到end之后、var之前即可。
 
此处若是在type...end之间声Divisor()函数,该怎么调用?

我用g:=TForm1.divisor(m,n);时报错"This form of method call only allowed for class methods"
 
...
function TForm1.Divisor(x,y:integer):integer;
var r:integer;
...

...
g:=divisor(m,n);
...
 
后退
顶部