在定义为(指针)函数类型的变量上获取必须具有(指针)函数类型

发布于 2024-11-03 19:01:29 字数 2287 浏览 4 评论 0原文

我正在为一个函数创建一个 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;
}

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

提前谢谢大家

I am creating a typedef for a function that will be used to call arbitrary functions that are stored in a string to function pointer map. I am sure that the problem has something to do with how the type is declared and referenced through many objects in my code but the is the only way I can think of how to do what I need to

This is fmap.h here i declare the fmap class and the executefunctions class, the typedef for the function pointer is the on the first line of the public members for 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

next is web.h I am only including what I think are the relevant parts

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

now what I think is the relavent part of web.cpp

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

Now finally. This is where I am getting the error. There are three lines of comments before the error line explaining the error I am getting.

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

any help anyone can give would be greatly appreciated.

Thank you all in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小矜持 2024-11-10 19:01:29

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

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

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

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

I'm not sure I didn't get lost reading the whole code but I believe you must call funcToCall() on a ExecFunctions instance, because ExecFunc is a method-pointer type.

What does it give if you use the following syntax:

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

or

ExecFunctions* ef = ...;
ef->*funcToCall(...);
不甘平庸 2024-11-10 19:01:29

您的 funcToCall 是一个指向成员函数的指针,但您像简单的函数指针一样调用,您需要在 ExecFunctions 类的实例上调用它,否则请考虑将 ExecFunctions 更改为命名空间,将 ExecFunc 更改为 int ()(Web *, Matlab *, Word *, int, string, vector; *);

Your funcToCall is a pointer-to-member-function but you are calling like a simple function pointer, you need to call it on an instance of the ExecFunctions class , otherwise consider changing ExecFunctions to a namespace and ExecFunc to int ()(Web *, Matlab *, Word *, int, string, vector<string> *);

開玄 2024-11-10 19:01:29

问题在于 C++ 中的成员函数和自由函数是不同的,如果您想到这一点,就会发现有一个很好的理由(隐藏的隐式 this 参数)。根据您的实际需要,您想要执行的操作有不同的解决方案:

低级别:

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

高级:

  • 使用 std::function (C++0x) 或 boost。 ::functionbind 一起使用:这是最灵活的,因为它不仅允许您绑定自由函数和静态函数,还允许绑定成员函数通过提供实际的对象但是:如果您不需要灵活性,您可以使用其他选项之一。

The issue is that member functions and free functions are different in C++, and if you think of it, there is a good reason (hidden implicit this argument). There are different solutions for what you want to do depending on what you really need:

Low level:

  • Make the functions non-member (move them to namespace level).
  • Make the functions static (removes the hidden this, and then that type defintion will work. Consider the first option first, C++ does not force you to add all code inside classes, and sometimes functions do not belong to a class. There is no point in creating a meaningless class just to store some free functions.
  • Change the typedef to reflect that they are members: typedef return_type (type::*member_function)( arg_type ); -- but this will require creation of a type object to call the member function on.

High level:

  • use std::function (C++0x) or boost::function together with bind: this is the most flexible, as it will allow you to bind not only free functions, and static functions but also member functions by providing the actual object. But: if you don't need the flexibility you can use one of the other options.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文