0

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?

folibis
  • 12,048
  • 6
  • 54
  • 97

1 Answers1

4

Unspecialized templates need to be implemented in the header file. If you put the implementation of Common::EnumToString inside common.h it will work.

amc176
  • 1,514
  • 8
  • 19