<分区>
我对模板很陌生,所以如果我的代码有很大错误,请不要对我太苛刻 :)
这是使用模板的类 Key
的头文件:
//Key.h
#ifndef KEY_H
#define KEY_H
#include "../Constants.h"
template<class KeyType>
class Key
{
public:
Key<KeyType>(KeyType initial);
KeyType increment();
private:
KeyType current;
};
#endif /* KEY_H */
这是 Key
类的 .cpp 文件:
//Key.cpp
#include "Key.h"
template<class KeyType> Key<KeyType>::Key(KeyType p_initial)
{
this->current = p_initial;
}
template<class KeyType> KeyType Key<KeyType>::increment()
{
this->current ++; //KeyType should implement this operator
}
那么问题是什么?我尝试在我的代码中的其他地方创建一个 Key
实例,如下所示:
Key songID (0); // ERROR: undefined reference to
Key<int>::Key(int)
然后使用
songID.increment(); // ERROR: undefined reference to
Key<int>::increment()