我正在尝试获取模板的基本视图,但我很困惑。我有 3 个类,每个类都基于模板:
#include <map>
using namespace std;
typedef void (*cb1)(int a, int b);
typedef void (*cb2)(int a, int b, int c);
template <class H>
class CHandle{
H m_id;
};
template <typename H>
class HndFactory {
public:
CHandle<H>* createHandle() {
return new CHandle<H>();
}
};
template <typename H>
CHandle<H> *createHandle(){
CHandle<H> *h = new CHandle<H>();
h->m_id = 100;
return h;
}
template <class S>
class CSubscriber{
S m_proc;
};
template <class H, class S>
class CEvent{
H m_handle;
S m_subscriber;
std::map<CHandle<H>, CSubscriber<S> > m_map;
public:
void subscribe(CHandle<H> *h);
void unsubscribe(CHandle<H> *h);
};
template <class H, class S>
void CEvent<H, S>::subscribe(CHandle<H> *h){
}
int main(void){
HndFactory<int> f;
CSubscriber<cb1> s;
CHandle<int> *h = f.createHandle();
CEvent<CHandle<int>, CSubscriber<cb1> > myevent;
myevent.subscribe(h);
return 0;
}
当我试图运行方法“ myevent.subscribe
”时,我得到了这个编译器错误:
CGlue.cpp: In function âint main()â:
CGlue.cpp:64: error: no matching function for call to âCEvent<CHandle<int>, CSubscriber<void (*)(int, int)> >::subscribe(CHandle<int>*&)â
CGlue.cpp:54: note: candidates are: void CEvent<H, S>::subscribe(CHandle<H>*) [with H = CHandle<int>, S = CSubscriber<void (*)(int, int)>]
我该如何以正确的方式调用此方法?我认为当我创建对象时h
它已经定义了类型?
最好的问候J。