无法使用Visual C++链接到带有模板的类

bas*_*ibe 1 c++ templates linker-errors

我在C++中编写了一个很好的小数组类,用于void*保存其条目.

现在,我想让它使用模板.这是我的新标题:

template <typename T>
class SimpleArray
{
public:
    SimpleArray();
    ~SimpleArray();

    void SetEntry(int idx, T setEntry);
    T GetEntry(int idx);
    // and so on
protected:
    T *pData
    short iNumEntries;
}
Run Code Online (Sandbox Code Playgroud)

这些函数在不同的文件中实现,如下所示:

#include "SimpleArray.h"

template <typename T>
void SimpleArray<T>::SetEntry(int idx, T setEntry)
{
    // code here
}

template <typename T>
T SimpleArray<T>::GetEntry(int idx)
{
    // more code here
}
Run Code Online (Sandbox Code Playgroud)

这编译很好,但是当我想在其他一些代码中使用它时

SimpleArray<SomeType*> cache;
cache.SetEntry(0, someThing);
Run Code Online (Sandbox Code Playgroud)

我收到一个链接器错误,声明有一个 unresolved external symbol

2> Thing.obj:错误LNK2019:未解析的外部符号"public:bool __thiscall SimpleArray :: SetEntry(int,class someThing*)"(?SetEntry @?$ SimpleArray @ PAUsHandle @@@@ QAE_NHPAUsHandle @@@ Z)在function"public:void __thiscall Thing :: Function(int)"(?DelEntry @ Thing @@ QAEXH @ Z)

伙计,我讨厌链接器甚至不试图说出任何可理解的东西.
无论如何,真正的麻烦是我在这里做了一些错误来破坏链接器.

你能告诉我我做错了什么吗?

unq*_*ind 7

您需要将所有代码放在头文件中.C++不能有效地支持单独的模板编译.