내가 선택 정렬을 구현하기 위해 시도하고 있지만 (아래 인쇄) 오류가 계속. 내 모든 포함 및 템플릿이 올바르게 수행되었다고 생각됩니다. 누군가이 오류에 대한 이유와이 유형의 오류를 디버깅하는 일반적인 방법을 설명 할 수 있습니까? 일반적으로 포함 또는 템플리트 문제가있을 때 발생하는 것으로 보이지만 때로는 무엇이 잘못되었는지를 모르는 상황에서 발생합니다. 감사.
오류 LNK2019 : 확인되지 않은 외부 기호 "공개 : 무효 __thiscall 선택 : 선택 정렬 (INT * const를, INT)"기능에서 참조 (? 선택 정렬 @ $ 선택 @ H @@ QAEXQAHH @ Z) _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];
}
}
SelcectionSort.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 방금 – PiotrNycz