template <class t =“”> error:'template'之前的预期初始化程序</class>

时间:2012-05-25 03:51:36

标签: c++ initializer

你好stackoverflow论坛的人, 我直接从教科书Absolute C ++ Fourth Edition Savitch ISBN-13:978-0-13-136584-1中输入了这段代码。 通用排序功能。 第728页的sort.cpp给出了第17行的错误: 第17行:错误:'模板'之前的预期初始化程序

有人可以提供帮助,因为我希望教科书“正常工作”,这样我就可以学习代码而不会陷入我不理解的额外错误。 是的,我已经研究过,但是这个错误的研究是有限的,因为我专注于通用排序功能的简单学习点,希望学习通用模板,希望学习哈希表... phewww,深吸一口气。

我无法对出现错误的第17行进行粗体显示。

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

2 个答案:

答案 0 :(得分:5)

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

在声明函数;之后,您似乎错过了swapValues()

另一方面,我不知道为什么函数声明悬挂在两个函数定义之间,尤其是之后使用它的函数。

答案 1 :(得分:0)

我想你错过了这里的分号

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