Reputation: 3079
I'm trying to get basic view of templates and i'm very confused. I have 3 classes, each based on template:
#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;
}
when i'm trying to run method "myevent.subscribe
" i got this compiler error:
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)>]
How shall i call this method in a proper way ? I tought that when i create object 'h
' it has already defined type ?
best regards J.
Upvotes: 0
Views: 82
Reputation: 14039
You either need to change your class members to
std::map<H, S> m_map;
void subscribe(H *h);
void unsubscribe(CHandle<H> *h);
or
Your CEvent
object should be declared as
CEvent<int, cb1> myevent;
You have to make one of these 2 changes.
Your current code does the template thing twice. With
template <class H, class S>
class CEvent{
and
CEvent<CHandle<int>, CSubscriber<cb1> > myevent;
what you have is
H == CHandle<int> & S = CSubscribe<cb1>
So
void subscribe(CHandle<H> *h);
becomes
void subscribe(<CHandle<CHandle<H> > * h);
once the template is instantiated.
Upvotes: 0
Reputation: 96241
The error message actually tells you what's wrong here with a little digging into its details:
candidates are: void CEvent<H, S>::subscribe(CHandle<H>*) [with H = CHandle<int>
From this we see that the candidate method is listed, with template type and the type being deduced for H
. When you substitute H with the type in question you get a candidate of:
void CEvent<H, S>::subscribe(CHandle<CHandle<int> >*)
which now hopefully clearly doesn't match the CHandle<int>
being passed in.
Instead, I think you want to use CEvent<int, cb1> myevent;
as your event object. Note that you may wish to use a typedef for the various handle and subscriber types within the CEvent
class as it may make future maintenance easier.
Upvotes: 0
Reputation: 157354
CEvent<CHandle<int>, CSubscriber<cb1> > myevent;
should be
CEvent<int, cb1> myevent;
Upvotes: 2