0

我在 .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)’

我很困惑,我在网上阅读了模板上的常见问题解答,但无法理解我的错误。如果你帮助我,你会让我开心...

4

1 回答 1

0

模板函数定义不能出现在常规 .cpp 中,因为它们在其他 .cpp 中对编译器不可用。将类定义分成 .h 和 .cpp 仍然很好,因此一种解决方案是#include "myclass.cpp"在 .h 文件的底部。

于 2012-07-21T07:41:15.893 回答