PDA

View Full Version : Problem with template with static QMap



marpasternak
9th April 2014, 17:58
Hi, I have a problem with template in which I use QMap:

template
<
class IdentifierType,
class BytesToRead
>
class CommandHandler : public Command
{
public:
explicit CommandHandler();
~CommandHandler();

static bool registerCommand(const IdentifierType& identifier, Command* ptrToObject);
static bool unregisterCommand(const IdentifierType& identifier);
QPair<ResponseType::type, QByteArray> handleMessage(QByteArray* datagramPtr);

private:
static QMap<IdentifierType, Command*> _associations;
};



template <class IdentifierType, class BytesToRead>
bool
CommandHandler<IdentifierType, BytesToRead>
::registerCommand(const IdentifierType& identifier, Command* ptrToObject)
{
if(_associations.contains(identifier))
return 0;
else
{
_associations.insert(identifier, ptrToObject);
return 1;
}
}


namespace TransportLayerType {
enum type{
V1 = 1
};
}

typedef CommandHandler<TransportLayerType::type, quint8> TransportLayerHandler;

And when I try to do this:

TransportLayerHandler::registerCommand(TransportLa yerType::V1, new TransportLayerV1());
I get:

undefined reference to `CommandHandler<TransportLayerType::type, unsigned char>::_associations'

Do you have any ideas?

Thanks for help, Marcin.

stampede
9th April 2014, 18:24
static template data member should be declared too (just like for regular, non-template classes), something like:


template<typename T, typename U>
QMap<T, Command*> CommandHandler<T,U>::_associations;

Put it somewhere after the template definition.

marpasternak
9th April 2014, 18:30
Wow, it works! Thanks a lot!