1

I need to make a function which adds a function pointer to map. It would be something like this:

bool RegisterFunction (string name, FunctionPointer function).

But I have problem with calling it, because I don't know how to pass function to it instead of result of function (when I call this as here:

RegisterFunction ("run", Run()) 

it doesn't works, neither works Run without parentheses, nor:

  • *Run()
  • &Run()
  • *Run
  • &Run

How to fix this?

Edit:
The error is:

parser.cpp|9|error: no matching function for call to  
    'MCXJS::Parser::RegisterFunction(const char [4], <unresolved overloaded function type>)'| 

The RegisterFunction() and Run() functions are in Parser class, which is in MCXJS namespace.
Class body is:

class Parser  
{  
    public:  
    Parser ();  
    CVariable RegisterFunction (FunctionPointer);  
    bool RegisterErrorHandler (ErrorType, ErrorHandlerPointer);  
    CVariable Run (std::string);  
    bool AlwaysDefaultErrorHandler;  
    int MaxCallStackSize;  
    private:  
    std::map <std::string, FunctionPointer> ExternalFunctions;  
    std::map <ErrorType, ErrorHandlerPointer> ErrorHandlers;  
    ErrorHandlerPointer DefaultErrorHandler;  
};  

And the parser.cpp file:

Parser::Parser ():  
    AlwaysDefaultErrorHandler (true), MaxCallStackSize (4)  
{  
    RegisterFunction ("run", Run);  
};  

CVariable Parser::Run (std::string path)  
{  
    return 5;  
};  

Typedefs:

typedef CVariable (*FunctionPointer) (std::string);  
typedef void (*ErrorHandlerPointer) (ErrorData);  
5
  • What does the declaration of RegisterFunction look like, what is FunctionPointer, and how is Run declared? Commented Aug 23, 2010 at 19:10
  • 1
    &Run or Run should both work if a declaration of Run is visible (and it is indeed a function). Can you update your question with all the relevant types and declarations? Commented Aug 23, 2010 at 19:11
  • What is the definition of 'FunctionPointer' and 'Run' Commented Aug 23, 2010 at 19:14
  • OK, post edit. You can't pass Run to a function expecting a normal function pointer because Run is a non-static member function. Pointers-to-members and function pointers are very different types and you can't use one where the other is expected. Commented Aug 23, 2010 at 19:30
  • Could you give some more examples of external functions? Do they have to be non-static member functions? How many do you expect to have and what percentage of them will be non-static. Where are you going to keep references or pointers to instances of classes those non-static member functions are about to work on? Did you think about it? What other classes will be involved? Commented Aug 23, 2010 at 20:25

3 Answers 3

3

Run is a non-static member function, not a normal function so either you are registering something of the wrong type or you need to change your typedef to refer to a pointer-to-member.

E.g.

typedef CVariable (Parser::*FunctionPointer) (std::string);

Then the correct way to form a pointer-to-member would be:

RegisterFunction("run", &Parser::Run);

Note that you have to use either the .* or ->* operator with an object or object pointer respectively to call the member function through the pointer-to-member.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to make pointer to non-static member of any class, not only this one?
A pointer to member is always associated with the class type itself. You can make a pointer to base class member and use it with instances of derived classes but you can't use a pointer-to-member with unrelated class instances. Pointers-to-member respect types.
1
RegisterFunction ("run", Run)

is the correct method. What error are you getting using that?

I suspect that the problem is not with how you are calling RegisterFunction, but with how you are defining it. You give us this:

RegisterFunction (string name, FunctionPointer function). 

but leave out the declaration of FunctionPointer. It would need to be defined something like:

typedef void (*FunctionPointer)()

Assuming that Run is defined as:

 void Run();

Note that for this to work, All of the functions you use with RegisterFunction must have the same signature.

UPDATE: Based on the error message you provided in the comment, it seem the problem is that you have more than one "Run" function, and the compiler don't know which one you want to pass. (Unfortunately, I'm not sure how you clarify it, so you may wish to rename one of them)

One thing you may want to try is:

 RegisterFunction (std::string("run"), Run);

Given an exact match for the first parameter, it may be able to choose which Run function based on which matches the signature in FunctionPointer.

UPDATE2 : You will either need to make Parser::Run() a static function, are change the declaration of FunctionPointer to:

 typedef CVariable (Parser::*FunctionPointer) (std::string); 

2 Comments

The error is: parser.cpp|9|error: no matching function for call to 'MCXJS::Parser::RegisterFunction(const char [4], <unresolved overloaded function type>)'|
typedef CVariable (*Parser::FunctionPointer) (std::string); is not the correct form for a pointer-to-member, I think you need to check your syntax.
0

You can just pass the name of the function without any decoration or any parentheses:

RegisterFunction(name, Run);

However, the following should also work:

RegisterFunction(name, &Run);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.