Linking error to base class's constructor when using templates

J

Jaco Naude

Hi again,

I will try to post as much information as possible about my error this
time. I am trying to develop a Subject/Observer system (as described
here http://www.codeproject.com/KB/architecture/observer_with_templates.aspx).
Everything works and compiles up to the point where my dll is created
and it fails with the following linking error:

(.text$_ZN11DataManagerC1Ev[DataManager::DataManager()]+0xd):
undefined reference to `Observer<SubjectType<IData*> >::Observer()'

The data manager class is defined as follows and is located along with
all the Observer classes etc. in a different library (dll) which I'm
sure works because I can link to other functions in that dll:

class DataManager : public Observer<SubjectType<IData*> >
public:
//! Object constructor.
DataManager() {}
//! Object desctructor.
~DataManager() {}

The Observer class is defined as follows:

template <class T>
class ObserverData;

template <class T>
class Observer {
public:
//! Object constructor.
Observer();
//! Object desctructor.
virtual ~Observer();

private:
ObserverData<T>* d;
};

And the implementation of the Observer class is done like this:
template <class T>
class ObserverData {
public:
ObserverData() {}
~ObserverData() {}
QMap<QString,T*> object_map;
int subject_limit;
};

//! Object constructor.
template <class T>
Observer<T>::Observer() {
d = new ObserverData<T>();
d->subject_limit = 1;
}

//! Object desctructor.
template <class T>
Observer<T>::~Observer() {
delete d;
}

I've been trying to figure this out for quite some time now and can't
figure out why it does not work. Am I inheriting correctly from the
Observer class?

Please help. Thanks for your time again.
Jaco
 
A

anon

Jaco said:
Hi again,

I will try to post as much information as possible about my error this
time. I am trying to develop a Subject/Observer system (as described
here http://www.codeproject.com/KB/architecture/observer_with_templates.aspx).
Everything works and compiles up to the point where my dll is created
and it fails with the following linking error:

(.text$_ZN11DataManagerC1Ev[DataManager::DataManager()]+0xd):
undefined reference to `Observer<SubjectType<IData*> >::Observer()'

This might help:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
The data manager class is defined as follows and is located along with
all the Observer classes etc. in a different library (dll) which I'm
sure works because I can link to other functions in that dll:

class DataManager : public Observer<SubjectType<IData*> >
public:
//! Object constructor.
DataManager() {}
//! Object desctructor.
~DataManager() {}

The Observer class is defined as follows:

template <class T>
class ObserverData;

template <class T>
class Observer {
public:
//! Object constructor.
Observer();
//! Object desctructor.
virtual ~Observer();

private:
ObserverData<T>* d;
};

And the implementation of the Observer class is done like this:
template <class T>
class ObserverData {
public:
ObserverData() {}
~ObserverData() {}
QMap<QString,T*> object_map;
int subject_limit;
};

//! Object constructor.
template <class T>
Observer<T>::Observer() {
d = new ObserverData<T>();
d->subject_limit = 1;
}

//! Object desctructor.
template <class T>
Observer<T>::~Observer() {
delete d;
}

I can not compile your example
 
J

Jaco Naude

Jaco said:
Hi again,
I will try to post as much information as possible about my error this
time. I am trying to develop a Subject/Observer system (as described
herehttp://www.codeproject.com/KB/architecture/observer_with_templates.aspx).
Everything works and compiles up to the point where my dll is created
and it fails with the following linking error:
(.text$_ZN11DataManagerC1Ev[DataManager::DataManager()]+0xd):
undefined reference to `Observer<SubjectType<IData*> >::Observer()'

This might help:http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13


The data manager class is defined as follows and is located along with
all the Observer classes etc. in a different library (dll) which I'm
sure works because I can link to other functions in that dll:
class DataManager : public Observer<SubjectType<IData*> >
public:
    //! Object constructor.
    DataManager() {}
    //! Object desctructor.
    ~DataManager() {}
The Observer class is defined as follows:
template <class T>
class ObserverData;
template <class T>
class Observer  {
public:
    //! Object constructor.
    Observer();
    //! Object desctructor.
    virtual ~Observer();
private:
    ObserverData<T>* d;
};
And the implementation of the Observer class is done like this:
template <class T>
class ObserverData {
public:
    ObserverData() {}
    ~ObserverData() {}
    QMap<QString,T*> object_map;
    int subject_limit;
};
//! Object constructor.
template <class T>
Observer<T>::Observer()  {
    d = new ObserverData<T>();
    d->subject_limit = 1;
}
//! Object desctructor.
template <class T>
Observer<T>::~Observer() {
    delete d;
}

I can not compile your example

Anon

Thank you very much. I'll remember that FAQ as I was not aware of it.
The suggestions work now.

Thanks again,
Jaco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top