0

I have been reading up on creating template classes and I think I have it.. well, apart from one annoying bug I can't figure!

this is what I have so far:

template<class T>
class CStateMachine
{
public:
    // Constructor
    CStateMachine(QByteArray smName);

private:
    QByteArray _smName;
};

// Here is the constructor implementation
template <class T>
CStateMachine<T>::CStateMachine(QByteArray smName):
    _smName(smName)
{
    qDebug() << "New statemachine:" << _smName << endl;
}

// Now here is the usage called from normal class CRpeComms:
#include "cstatemachine.h"
...
CStateMachine<CRpeComms> rpeSm("test");
...

This generates an error:

unidentified reference to CStateMachine<CRpeComms>::CStateMachine(QByteArray)

Note: I am using Qt so if you are not familiar with that then think of qDebug() and std::out or similar and QByteArray as CString or similar.

I have looked at lots of example and mine seems identical. If I move the implementation of my constructor into the header file (like below) then it works no problem...:

CStateMachine(QByteArray smName):
    _smName(smName)
{
    qDebug() << "new statemachine:" << _smName << endl;
}

So I am not sure what I am doing wrong here...

Any help much appreciated!

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • @n.m., will check that post out now :) – code_fodder Jun 12 '13 at 12:24
  • @Andy, yes it is in a separate file, is that a problem? – code_fodder Jun 12 '13 at 12:25
  • Ok, I have read that "possible duplicate" link... it looks like I need to move all my "code" from the .cpp file into the header file (well, that seems to be the easy method). Do people agree with that? Thanks :) – code_fodder Jun 12 '13 at 12:30
  • Either define it into the header or use explicit template instantiation (see [here](http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc16explicit_instantiation.htm)) – 3XX0 Jun 12 '13 at 12:37
  • Cool!, it works nicely if I put this all in the header :) ... Explicit template instantiation just looks even more mind boggling... I think I will leave that for another day :o Thanks for the comments, it has got me where I want to go :) I'd mark you all +1 if I could. – code_fodder Jun 12 '13 at 12:41

0 Answers0