3

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;
}
Perception
  • 79,279
  • 19
  • 185
  • 195
user1416486
  • 61
  • 2
  • 7

2 Answers2

6
template<class T>
void swapValues(T& variable1, T& variable2);
                                         ^^^^^^
template<class T>
int indexOfSmallest(const T a[], int startIndex, int numberUsed)

It seems you are missing a ; after declaration of the function swapValues().

On a side note, I don't know why the function declaration is placed dangling between two function definitions, especially after the function which uses it.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Thanks for your reply. I tried that, however it seems a follow through with an unclear note in the text book fixed the issue. I have added a body which is located on page 719 from link 8 to 14. { T temp; temp = variable1; variable1 = variable2; variable2 = temp; } It finally compiles. I find that programming text books need help with 'user friendly' learning, as this text book note to add the body is not clear and only buries learners deeper in the abyss of confusion. – user1416486 May 25 '12 at 23:01
  • @user1416486: I don't have the book neither I have ever used it so I can't comment on it but it seems like a pretty badly formatted book from your description.I would suggest you drop and pick yourself a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Alok Save May 26 '12 at 12:20
0

I think you missed the semicolon here

void swapValues(T& variable1, T& variable2);

ravi
  • 3,304
  • 6
  • 25
  • 27