【问题标题】:Unresolved External Symbol -- Template Class [duplicate]未解析的外部符号——模板类
【发布时间】:2012-10-11 21:35:01
【问题描述】:

可能重复:
C++ template, linking error

我正在尝试实现选择排序,但我不断收到错误消息(打印在下面)。在我看来,我所有的包含和模板都正确完成。有人可以向我解释此错误的原因以及调试此类错误的一般方法。它通常似乎发生在存在包含或模板问题时,但偶尔会发生在我不知道出了什么问题的情况下。谢谢。

错误 LNK2019:函数 _main 中引用了无法解析的外部符号“public: void __thiscall Selection::SelectionSort(int * const,int)”(?SelectionSort@?$Selection@H@@QAEXQAHH@Z)

test.cpp

#include <iostream>
#include "SelectionSort.h"
using namespace std;

void main()
{
    int ar[] = {1,2,3,4,5};
    Selection<int> s;
    s.SelectionSort(ar,5);

    for(int i = 0; i < 5; i++)
    {

        cout << "\nstudent number " << i + 1<< " grade " << ar[i];
    }
}

选择排序.h

template<class ItemType>
class Selection
{
public:
    void SelectionSort(ItemType[], int);
private:
    int MinIndex(ItemType[], int, int);
    void Swap(ItemType& , ItemType&);
};

SelectionSort.cpp

#include "SelectionSort.h"

template<class ItemType>
void Selection<ItemType>::SelectionSort(ItemType values[], int numValues)
// Post: The elements in the array values are sorted by key.
{
int endIndex = numValues-1;
for (int current = 0; current < endIndex; current++)
Swap(values[current],
values[MinIndex(values, current, endIndex)]);
}

template<class ItemType>
int Selection<ItemType>::MinIndex(ItemType values[], int startIndex, int endIndex)
// Post: Returns the index of the smallest value in
// values[startIndex]..values[endIndex].
{
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <= endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}

template<class ItemType>
inline void Selection<ItemType>::Swap(ItemType& item1, ItemType& item2)
// Post: Contents of item1 and item2 have been swapped.
{
ItemType tempItem;
tempItem = item1;
item1 = item2;
item2 = tempItem;
}

【问题讨论】:

  • @DavidRodríguez-dribeas 刚刚做了

标签: c++ templates


【解决方案1】:

SelectionSort.cpp 的内容移动到SelectionSort.h,就在类声明的下方。还要确保在整个 .h 文件的内容周围有一个标头保护。

问题来自 C++ 如何实现模板。每次它看到与模板类一起使用的新类型(如Selection&lt;int&gt;)时,它都会重新创建整个类,将ItemType 替换为int

因此,它需要在编译时知道类的完整定义(连同它的方法)。它不能只使用类定义并将链接延迟到以后。

【讨论】:

  • 感谢您的回复。我之前使用过模板,同时将我的代码分成声明和实现文件。当我回顾那些项目时,我找不到任何不同之处。你知道是什么让之前的项目编译了吗?
  • 我只记得我做了什么让它们编译。我添加了包含防护,还在测试驱动程序中包含了 .cpp 实现文件。那如何改变编译以使一切正常?
  • @Azzi,底线是编译器需要能够在编译test.cpp 文件时看到模板类的整个实现。包含 CPP 文件是可行的,因为编译器随后可以访问完整的实现细节,但我总是发现这比它的价值更令人困惑。
猜你喜欢
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2012-03-11
  • 2014-06-13
  • 2013-05-25
  • 2014-04-03
  • 2016-06-19
相关资源
最近更新 更多