2015-10-05 45 views
0

在一個文件中,我有一段代碼是這樣的:功能作爲參數傳遞會導致E2035錯誤

// foo.pas 
unit Foo; 

type 
FooExecutorMethod = function (param: Pointer) : Integer of Object; 

FooExecutor = class 
private 
    FMethod: TiTefExecutorMethod; 
    FMethodParam: Pointer; 
    FMethodResult: Integer; 
public 
    function RunMethod(method: TiTefExecutorMethod; param: Pointer) : Integer; 
end; 

implementation 

function FooExecutor.RunMethod(method: FooExecutorMethod; param: Pointer) : Integer; 
begin 
    FMethod:= method; // Never gets assigned :(
    FMethodParam:= param; 

    // Some threading logic here. 
end; 

在同一個項目中的另一個文件,我得到了這樣的事情:

// bar.pas 
uses Foo; 

type 
bar = class 
protected 
    executor: FooExecutor; 
    function doSomething(params: Pointer): Integer; 
    Constructor Create(params: Pointer); 

implementation 

Function bar.doSomething(params: Pointer) : Integer; 
begin 
    // Do something with the pointer 
    Result := 1; 
end; 

Constructor bar.Create(params: Pointer) 
var 
    r: Integer; 
begin 
    executor := FooExecutor.Create 
    r := executor.RunMethod(doSomething, params); 
end; 

問題是,bar.DoSomething從未得到執行。在調試FooExecutor.RunMethod,評價者不顯示其「method」參數的值 - 它顯示改爲以下內容:

E2035沒有足夠的實際參數

就這樣我無法使用參考到bar.DoSomething

我在做什麼錯?

+0

您不會顯示您的TiTefExecutorMethod聲明。這只是一個應該讀取FooExecutorMethod的錯字嗎?錯誤E2035可能沒有什麼可擔心的 - 它只是表達式求值器試圖執行的功能,當然這是不行的。而且,正如David Heffernan所評論的,在RunMethod中,您只需存儲參數即可。你實際上沒有對他們做任何事情。 – Dsm

回答

0

該方法從未執行,因爲你永遠不會調用它。你所做的只是傳遞一個對方法的引用。但你永遠不會這麼稱呼它。

你會這樣稱呼它:

function FooExecutor.RunMethod(method: FooExecutorMethod; param: Pointer): Integer; 
begin 
    Result := method(param); 
end; 

顯然你的代碼試圖做其它的事情,但是這是如何調用該方法。

相關問題