2012-04-24 71 views
1

我有这个函数的声明和实现如何在Delphi上正确定义函数?

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' 

如何解决这个问题?

+0

为了让您的工作更轻松,在界面部分中定义函数之后,请按下Ctrl-Shift-C在实现部分中正确自动定义函数。 – Justmade 2012-04-25 02:55:09

+0

@Justmade,感谢您的快捷:) – Shirish11 2012-04-25 05:05:07

+0

@Justmade,如果你不使用Delphi XE2 :( – Branko 2012-04-25 09:59:15

回答

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;