我在.h文件中有以下课程:
class T;
template < class Class, typename ReturnType,typename Parameter1 = int , typename Parameter2 = int, typename Parameter3 = int, typename Parameter4 = int >
class EventHandlerAbstract
{
public:
// constructors and destructors
EventHandlerAbstract();
// ~EventHandlerAbstract();
// data types
typedef ReturnType (Class::*Method)(Parameter1, Parameter2, Parameter3, Parameter4);
// test,int Method
Method_ ;
EventHandlerAbstract(Class* _class_instance, Method _method)
{
class_instance = _class_instance;
method = _method;
}
ReturnType operator()(Parameter1 parameter1, Parameter2 parameter2=0, Parameter3 parameter3=0, Parameter4 parameter4=0)
{
return (class_instance->*method)(parameter1, parameter2, parameter3, parameter4);
}
ReturnType execute(Parameter1 parameter1, Parameter2 parameter2, Parameter3 parameter3, Parameter4 parameter4)
{
return operator()(parameter1, parameter2, parameter3, parameter4);
}
static void * makeFunctionPointer(Method _method, Class* _instance_class,Parameter1 parameter1 ,Parameter2 parameter2 , Parameter3 parameter3 , Parameter4 parameter4 );
Class* class_instance;
Method method;
static Class callbackType;
static pthread_t threadArray[1];
static std::vector<void *> callbackList ;
static std::vector<std::string> eventList ;
static std::vector<std::pair <int, int> > eventRelationToCallbackList;
static std::vector<void * > runningCallbackList; // it should to free and thread should free it .
static std::vector<std::string > runningEventList; // it should to free and thread should free it .
}
然后,我在cpp文件中有以下功能:
template <class Class,typename ReturnType,typename Parameter1 ,typename Parameter2 ,typename Parameter3 ,typename Parameter4 >
void * EventHandlerAbstract<Class,ReturnType,Parameter1,Parameter2,Parameter3,Parameter4>::makeFunctionPointer(Method _method, Class* _instance_class,Parameter1 parameter1 = 0,Parameter2 parameter2 = 0, Parameter3 parameter3 = 0, Parameter4 parameter4 = 0)
{
void * tempCallback;
///add_callback
//
typedef typeof(_instance_class) _Class ;
typedef EventHandlerAbstract<_Class, ReturnType, Parameter1, Parameter2, Parameter3, Parameter4> Callback_;
// _Class _instance = _Class();
Callback_ classPointer(&_instance_class, &_method);
EventHandlerAbstract<test,int>::callbackType = classPointer ;
tempCallback = (static_cast<void*>(&classPointer));
//(*static_cast< _callback* >(tempCallback))(1,2,3,4); //running a func, a good test
return tempCallback;
}
但是当我在主要功能中调用时,如下面的调用,我得到错误, 我的呼唤:
cout << (void *)(EventHandlerAbstract<T,T>::makeFunctionPointer(&test::called,ptr,1, 2, 5, 9)) << endl;
或:
cout << (void *)(EventHandlerAbstract<test,bool>::makeFunctionPointer(&test::called,ptr,1, 2, 5, 9)) << endl;
我的错误:
error: no matching function for call to ‘EventHandlerAbstract<T, T>::makeFunctionPointer(int (test::*)(int, int, int, int), test&, int, int, int, int)’
或
error: no matching function for call to ‘EventHandlerAbstract<test, bool>::makeFunctionPointer(int (test::*)(int, int, int, int), test&, int, int, int, int)’
我很困惑,我在网上阅读了模板上的FAQ,但无法理解我的错误。 如果帮助我,你会让我高兴...
答案 0 :(得分:0)
模板函数定义不能出现在常规.cpp中,因为它们在其他.cpps中不可用于编译器。将类定义分成.h和.cpp仍然很好,因此一种解决方案是在.h文件的底部放置#include "myclass.cpp"
。