c++ - 具有外部错误的模板特化

标签 c++ templates

当我尝试专门化我的模板函数之一时,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/

相关文章:

c# - 如何在 ms word 模板中放置标签并用数据库中的数据填充它们

c++ - 有什么方法可以通过 SSH 连接打开和读取文件吗?

c++ - 如何覆盖运算符 <

c++ - 如何在 C++ 中管理 'ostringstream' 对象?

c++ - 格式说明符 C++ 有什么问题

swift - Xcode 项目模板和 Swift 包依赖

具有可变参数包的函数的 C++ 部分模板参数推导在 Clang 和 MSVC 中产生不明确的调用

c++ - 在模板函数中使用 STL Sort?

c++ - 将整数存储为 float

c++ - 非默认构造函数类型的继承构造函数 + 类内初始化失败