I have a class contains common functions for my project. One of function is template static:
common.h
#include <QMetaEnum>
#include <QString>
class Common
{
public:
Common();
template<typename T> static QString EnumToString(const T value);
};
and so implementation:
common.cpp
template<typename T>
QString Common::EnumToString (const T value)
{
return QString(QMetaEnum::fromType<T>().valueToKey(value));
}
That compiles without problems but when I want to use this functions like this:
MyEnum enum = MyEnum::Value1;
qDebug() << Common::EnumToString<MyEnum>(enum);
I get some strange linker error:
error: undefined reference to `QString Common::EnumToString(MyEnum)'
MyEnum has registered in Qt metasystem:
enum class MyEnum
{
Value1,
Value2,
Value3
};
Q_ENUM(MyEnum);
What I do wrong and how to get it work?