-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();
};

现在我认为是 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;
}

任何人都可以提供的任何帮助将不胜感激。

谢谢大家

4

3 回答 3

0

我不确定我没有迷失阅读整个代码,但我相信你必须调用funcToCall()一个ExecFunctions实例,因为ExecFunc它是一种方法指针类型。

如果您使用以下语法,它会给出什么:

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

或者

ExecFunctions* ef = ...;
ef->*funcToCall(...);
于 2011-04-28T07:28:59.227 回答
0

YourfuncToCall是一个指向成员函数的指针,但您调用的方式就像一个简单的函数指针,您需要在ExecFunctionsclass 的实例上调用它,否则考虑更改ExecFunctions为命名空间ExecFuncint ()(Web *, Matlab *, Word *, int, string, vector<string> *);

于 2011-04-28T07:32:49.963 回答
0

问题是 C++ 中的成员函数和自由函数是不同的,如果你仔细想想,这是有充分理由的(隐藏的隐式this参数)。根据您的实际需要,您想要做的事情有不同的解决方案:

低级:

  • 使函数成为非成员(将它们移动到命名空间级别)。
  • 使函数静态(删除隐藏的this,然后该类型定义将起作用。首先考虑第一个选项,C++ 不会强制您在类中添加所有代码,有时函数不属于一个类。没有意义创建一个无意义的类只是为了存储一些免费功能。
  • 更改 typedef 以反映它们是成员:typedef return_type (type::*member_function)( arg_type );- 但这需要创建一个type对象来调用成员函数。

高水平:

  • use std::function(C++0x) or boost::functiontogether with bind: 这是最灵活的,因为它bind不仅允许您通过提供实际对象来释放函数和静态函数,还允许您释放成员函数。但是:如果您不需要灵活性,您可以使用其他选项之一。
于 2011-04-28T07:33:24.797 回答