如何在Delphi上正确定义函数?

时间:2012-04-24 19:38:41

标签: delphi function delphi-7

我有这个函数声明和实现

public
function AddWordReference(wordId,translateId:Longint):Longint;
{***}
function AddWordReference(wordId,translateId:Longint):Longint;
begin
try
 if((wordId <> -1) OR (translateId <> -1))  Then
 begin
 DataModule1.TranslateDictionary.AppendRecord([nil,wordId,translateId]);
 DataModule1.TranslateDictionary.Last;
 AddWordReference := DataModule1.TranslateDictionary.FieldByName('Id').AsInteger;
 end;
Except
ShowMessage('Error wirh adding reference');
AddWordReference := -1;
end;
AddWordReference := -1;
end;

我有这个错误:

[Error] AddFormUnit.pas(34): Unsatisfied forward or external declaration: 'TForm2.AddWordReference'

如何解决此错误?

1 个答案:

答案 0 :(得分:12)

它是您的TForm2课程的成员,因此在实施部分中,您必须将其声明为TForm2.AddWordReference,而不仅仅是AddWordReference。然后在方法本身内部,您应该将返回值分配给编译器的Result变量而不是AddWordReference方法名称:

public
  function AddWordReference(wordId, translateId: Longint): Longint;

function TForm2.AddWordReference(wordId, translateId: Longint): Longint;
begin
  Result := -1;
  try
    if (wordId <> -1) OR (translateId <> -1) then
    begin
      DataModule1.TranslateDictionary.AppendRecord([nil, wordId, translateId]);
      DataModule1.TranslateDictionary.Last;
      Result := DataModule1.TranslateDictionary.FieldByName('Id').AsInteger;
    end;
  except
    ShowMessage('Error wirh adding reference');
  end;
end;