Sidd
Sidd

Reputation: 1208

C++ function pointer with templates

I'm trying to store a pointer to a member function. The class that needs to store the pointer is declared as:

template <typename TDataType, typename T>
bool my_function(std::string topic_name,
                 void(T::*)(const TDataType&) fp,
                 T* obj)

I get the error:

error: expected ',' or '...' before 'fp'
                               void(T::*)(const TDataType&) fp,

Can someone tell me what's going on? Looks like it's a syntax error that I don't get.

Upvotes: 0

Views: 43

Answers (2)

NathanOliver
NathanOliver

Reputation: 180594

The syntax for a member function pointer is

return_type(class_name::* function_pointer_name)(function_parameters)

So

template <typename TDataType, typename T>
bool my_function(std::string topic_name,
                 void(T::*)(const TDataType&) fp,
                 T* obj)

Needs to be

template <typename TDataType, typename T>
bool my_function(std::string topic_name,
                 void(T::* fp)(const TDataType&)'
                 T* obj)

Upvotes: 1

Artur Pyszczuk
Artur Pyszczuk

Reputation: 1930

Change:

void(T::*)(const TDataType&) fp

to

void(T::* fp)(const TDataType&)

Upvotes: 3

Related Questions