getting a must have (pointer-to) function type on a variable that is defined as a (pointer-to) function type
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 ma开发者_开发技巧p. 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
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(...);
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> *);
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 atype
object to call the member function on.
High level:
- use
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.
精彩评论