Hi stackoverflow forum people, I've typed up this code direct from the text book, Absolute C++ Fourth Edition Savitch ISBN-13: 978-0-13-136584-1. A Generic Sorting Function. sort.cpp on page 728 gives the error on line 17: Line 17: error: expected initialiser before 'template'
Could someone help as I would expect the text book to 'just work' so I can study the code and not get stuck on extra errors I don't understand. Yes, I have researched, however this research of the error is limited as I am concentrating on the simpler learning point of the Generic Sorting Function, in the hope of learning the Generic Template, in the hope of learning the hashtable...phewww, take a breath.
I am unable to bold line 17 where the error occurs.
// This is the file sort.cpp.
template<class T>
void sort(T a[], int numberUsed)
{
int indexOfNextSmallest;
for (int index = 0; index < numberUsed - 1; index++)
{//Place the correct value in a[index]:
indexOfNextSmallest =
indexOfSmallest(a, index, numberUsed);
swapValues(a[index], a[indexOfNextSmallest]);
//a[0] <= a[1] <=...<= a[index] are the smallest of the original array
//elements. The rest of the elements are in the remaining positions.
}
}
template<class T>
void swapValues(T& variable1, T& variable2)
template<class T>
int indexOfSmallest(const T a[], int startIndex, int numberUsed)
{
T min = a[startIndex];
int indexOfMin = startIndex;
for (int index = startIndex + 1; index < numberUsed; index++)
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
//min is the smallest of a[startIndex] through a[index].
}
return indexOfMin;
}