2015-11-06 123 views
0

我試圖存儲一個指向成員函數的指針。需要存儲指針的類被聲明爲:帶模板的C++函數指針

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

我得到的錯誤:

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

誰能告訴我這是怎麼回事?看起來像是我沒有得到的語法錯誤。

回答

1

爲一個成員函數指針的語法是

return_type(class_name::* function_pointer_name)(function_parameters) 

所以

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

需要

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

變化:

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

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