未解析的外部符号——模板类

未解析的外部符号——模板类

本文介绍了未解析的外部符号——模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
C++ 模板,链接错误

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

I am attempting to implement a selection sort, but I keep getting the error(printed below). It seems to me that all my includes and templates are done correctly. Can someone explain to me the reason for this error and the general approach to debugging this type of error. It usually seems to happen when there is an include or template issues, but occasionally it occurs in situation where I have no idea what is wrong. Thanks.

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

error LNK2019: unresolved external symbol "public: void __thiscall Selection::SelectionSort(int * const,int)" (?SelectionSort@?$Selection@H@@QAEXQAHH@Z) referenced in function _main

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

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;
}

推荐答案

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

Move the contents of SelectionSort.cpp to SelectionSort.h, just below the class declaration. Also ensure that you have a header guard around the contents of your entire .h file.

问题来自于 C++ 如何实现模板.每次看到模板类中使用的新类型(如 Selection)时,它都会重新创建整个类,用 int 替换 ItemType>.

The problem comes from how C++ implements templates. Every time it sees a new type used with a template class (like Selection<int>), it recreates the entire class, replacing ItemType with int.

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

For this reason, it needs to know the full definition of the class (along with its methods) at compile time. It cannot just use the class definition and delay linking until later.

这篇关于未解析的外部符号——模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:12