C ++未定义对使用模板的类的引用

时间:2013-04-14 06:08:01

标签: c++ templates compiler-errors undefined-reference

我对模板很新,所以如果我的代码非常错误,请不要苛刻我:) 这是使用模板的类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); //错误:对Key<int>::Key(int)

的未定义引用

然后使用

  

songID.increment(); //错误:对Key<int>::increment()

的未定义引用

1 个答案:

答案 0 :(得分:1)

两点:

相关问题