当我尝试专门化我的模板函数之一时,Visual Studio 向我抛出一个外部错误,包括一个未专门化的函数的错误。
三个错误:
1>------ Build started: Project: Project3, Configuration: Debug Win32 ------
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::reverse(void)" (?reverse@?$linearList@VFriendToken@@@@UAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::print(void)" (?print@?$linearList@VFriendToken@@@@UAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::insertionSort(void)" (?insertionSort@?$linearList@VFriendToken@@@@UAEXXZ)
这是代码的相关部分:
template<class T>
class arrayList : public linearList<T>
{
public:
//other methods
void reverse();
void print();
void insertionSort();
};
template<class T>
void arrayList<T>::reverse()
{
//method body
}
template<>
void arrayList<FriendToken>::insertionSort()
{
//method body
}
template<>
void arrayList<FriendToken>::print()
{
//method body
}
template<class T>
void arrayList<T>::insertionSort(){}
template<class T>
void arrayList<T>::print(){}
最佳答案
您的示例显示了 arrayList 成员函数的特殊化,我认为这些成员函数应该覆盖它们在 linearList 中的虚拟等价物。链接器说它无法在 linearList 类中找到虚拟成员,该类未包含在您的示例中。
virtual void __thiscall linearList<class FriendToken>::reverse(void)
如果我像这样添加 linearList 的定义,则链接器是安静的(在 MSVC2010 上,我还添加了一个空的 FriendToken 类以使其正常工作)。
template<typename T>
class linearList
{
public:
virtual void reverse() = 0; //pure virtual
virtual void print() = 0;
virtual void insertionSort() = 0;
};
如果这不是您的问题,请发布 linearList 的代码,我会更新我的答案,因为这肯定是您问题的根源。
如果需要引用这里是我如何使用函数reverse来测试:
arrayList<FriendToken> a;
static_cast<linearList<FriendToken>&>(a).reverse();
关于c++ - 具有外部错误的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14937858/