我想使用一个模板类来为一些非常相似的子类提供一些共同的功能。唯一的变化是每个子类使用的枚举类型。
这是父类。
这是其中一个孩子。
这是构造函数
链接器报错如下:
这是父类。
template<typename T> class E_EnumerationBase : public SimpleElement
{
public:
E_EnumerationBase();
virtual bool setValue(QString choice);
virtual T getState();
protected:
T state;
QHash<QString, T> dictionary;
};
template<typename T> E_EnumerationBase<T>::E_EnumerationBase() {
state = 0;
}
template<typename T> bool E_EnumerationBase<T>::setValue(QString choice) {
T temp;
temp = dictionary.value(choice, 0);
if (temp == 0) {
return false;
}
value = choice;
state = temp;
return true;
}
template<typename T> T E_EnumerationBase<T>::getState() {
return state;
}
这是其中一个孩子。
enum TableEventEnum {
NO_VALUE = 0,
ATTRACT = 1,
OPEN = 2,
CLOSED = 3
};
class E_TableEvent : public E_EnumerationBase<enum TableEventEnum>
{
public:
E_TableEvent();
};
这是构造函数
E_TableEvent::E_TableEvent()
{
state = NO_VALUE;
dictionary.insert("attract", ATTRACT);
dictionary.insert("open", OPEN);
dictionary.insert("closed", CLOSED);
}
链接器报错如下:
e_tableevent.cpp:6: error: undefined reference to `E_EnumerationBase<TableEventEnum>::E_EnumerationBase()'
像这样,可以将枚举用作模板的参数吗?