2011-04-28 94 views
-1

我正在爲函數創建一個typedef,該函數將用於調用存儲在字符串到函數指針映射。我相信這個問題與如何通過我的代碼中的許多對象聲明和引用類型有關,但這是我能想到如何做我需要的唯一方法在被定義爲(指針)函數類型的變量上獲得必須的(指針)函數類型

這是fmap.h在這裏,我聲明FMAP類和executefunctions班,爲的typedef函數指針是對公衆成員的FMAP

class Web; 
class Matlab; 
class Word; 
class Node; 

class ExecFunctions 
{ 
public: 
    ExecFunctions(){} 
    /** 
    Function:  travel 
    */ 
    int travel(Web *concepts, Matlab *mat, Word * words, int reqIdx, string theWord, vector<string> *dependsMet); 

}; 

class FMap 
{ 
public: 
    typedef int (ExecFunctions::*ExecFunc)(Web *, Matlab *, Word *, int, string, vector<string> *); 
    ExecFunc getFunc(string funcName){ 
     return theFuncMap.descrToFuncMap[funcName]; 
    } 

private: 
    class FuncMap { 
    public: 
     FuncMap() { 
       descrToFuncMap["travel"] = &ExecFunctions::travel; 
     } 
     std::map<std::string, ExecFunc> descrToFuncMap; 
    };  
}; 



#endif 

旁邊的第一行是web.h我只包括什麼,我認爲是相關部件

#include "fmap.h" 

class Node 
{ 
    private: 
     .... 
     //pointer to execcution function 
     FMap::ExecFunc func; 
     //fmap function used to access execution function pointers 
     FMap *functionMap; 
      .....  
public: 
      FMap::ExecFunc getExecFunc(); 
}; 

現在我認爲是t他相信web.cpp的一部分

Node::Node(string name, Node *nodeParent) 
{ 
    attrList = new Attr(); 
    nodeName = name; 
     ........ 
    func = functionMap->getFunc(name); 
} 

現在終於。這是我得到錯誤的地方。在錯誤行解釋我得到的錯誤之前,有三行註釋。

void process(Util myUtil, Web *concepts, Matlab *mat, string path) 
{ 
    int funcP 
    bool dependsProcessed = false; 
    vector<string> *dependsDone = new vector<string>(); 
    FMap::ExecFunc funcToCall; 

    funcToCall = realMeanings[i][j]->getConcept()->getExecFunc(); 


//the line bellow this comment is where I'm getting the error. Visual Studio says 
//that funcToCall must have (pointer-to) function type, and then the VS compiler says 
//diabot.cpp(177): error C2064: term does not evaluate to a function taking 6 arguments 
    funcPtrRet = funcToCall(concepts, mat, realMeanings[i][j], reqConceptsIdx[i][j], queryWords[i], dependsDone); 

    return; 
} 

任何人都可以給任何幫助將不勝感激。

謝謝大家提前

+1

這就是很多代碼,其中大部分代碼可能與問題無關。你可以嘗試簡化你的例子嗎? – 2011-04-28 07:26:13

+0

對不起,我無法將代碼分解爲基本問題。我把它削減了一下。讓我知道如果它仍然很多 – jeffminton 2011-04-28 07:35:27

回答

0

我不知道我沒有迷失在閱讀整個代碼,但我相信你一定叫上ExecFunctions實例funcToCall(),因爲ExecFunc是一個方法指針類型。

是什麼給,如果你使用的語法如下:

ExecFunctions ef; 
ef.*funcToCall(...); 

ExecFunctions* ef = ...; 
ef->*funcToCall(...); 
+0

第一個根本不會改變錯誤,它仍然給出一個必須有(指針)函數類型錯誤,第二個給出了無法分配類型FMap :: ExecFunc來鍵入ExecFunctions而ExecFunctions沒有成員funcToCall – jeffminton 2011-04-28 07:56:11

0

funcToCall不過是你在呼喚像一個簡單的函數指針的指針到成員函數,您需要在ExecFunctions類的實例上調用它,否則請考慮將ExecFunctions更改爲命名空間並將ExecFunc更改爲int()(Web *, Matlab *, Word *, int, string, vector<string> *);

0

問題是成員函數和自由函數在C++中是不同的,如果你想到它,有一個很好的理由(隱含的隱含的this參數)。有您要取決於你真正需要做的不同的解決方案:

低電平:

  • 製作的功能非成員(將它們移到命名空間的水平)。
  • 使函數靜態(去除隱藏的this,然後該類型定義將起作用。首先考慮第一個選項,C++不會強制您在類中添加所有代碼,有時函數不屬於類。創建一個無意義的類僅僅是爲了存儲一些自由函數是沒有意義的
  • 更改typedef以反映它們是成員:typedef return_type (type::*member_function)(arg_type); - 但這需要創建一個要調用成員函數的type對象。

高級別:

  • 使用std::function(C++ 0x中)或bindboost::function一起:這是最靈活的,因爲它可以讓你bind不僅是免費的功能,和靜態函數而且通過提供實際的對象來提供成員函數。但是:如果您不需要靈活性,則可以使用其他選項之一。
相關問題