在定义为(指针)函数类型的变量上获取必须具有(指针)函数类型
我正在为一个函数创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我没有迷失阅读整个代码,但我相信您必须在
ExecFunctions
实例上调用funcToCall()
,因为ExecFunc
code> 是方法指针类型。如果您使用以下语法,它会给出什么:
或
I'm not sure I didn't get lost reading the whole code but I believe you must call
funcToCall()
on aExecFunctions
instance, becauseExecFunc
is a method-pointer type.What does it give if you use the following syntax:
or
您的 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 theExecFunctions
class , otherwise consider changingExecFunctions
to a namespace andExecFunc
toint ()(Web *, Matlab *, Word *, int, string, vector<string> *);
问题在于 C++ 中的成员函数和自由函数是不同的,如果您想到这一点,就会发现有一个很好的理由(隐藏的隐式
this
参数)。根据您的实际需要,您想要执行的操作有不同的解决方案:低级别:
this
,然后该类型定义将起作用。首先考虑第一个选项,C++ 不会强制您在类中添加所有代码,有时函数不属于仅仅为了存储一些自由函数而创建一个无意义的类是没有意义的typedef return_type (type::*member_function)( arg_type );
- - 但这需要创建一个type
对象来调用成员函数高级:
std::function
(C++0x) 或boost。 ::function
与bind
一起使用:这是最灵活的,因为它不仅允许您绑定自由函数和静态函数,还允许绑定成员函数通过提供实际的对象但是:如果您不需要灵活性,您可以使用其他选项之一。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:
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.typedef return_type (type::*member_function)( arg_type );
-- but this will require creation of atype
object to call the member function on.High level:
std::function
(C++0x) orboost::function
together withbind
: this is the most flexible, as it will allow you tobind
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.