嗨stackoverflow论坛的人,我直接从教科书中输入了这段代码,Absolute C++ Fourth Edition Savitch ISBN-13:978-0-13-136584-1.通用排序功能.第728页的sort.cpp给出了第17行的错误:第17行:错误:'template'之前的预期初始化程序
有人可以提供帮助,因为我希望教科书"正常工作",这样我就可以研究代码而不会陷入我不理解的额外错误.是的,我已经研究过,但是这个错误的研究是有限的,因为我专注于通用排序功能的简单学习点,希望学习通用模板,希望学习哈希表... 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;
}
Run Code Online (Sandbox Code Playgroud)
template<class T>
void swapValues(T& variable1, T& variable2);
^^^^^^
template<class T>
int indexOfSmallest(const T a[], int startIndex, int numberUsed)
Run Code Online (Sandbox Code Playgroud)
看来你错过了;函数的声明swapValues().
另外,我不知道为什么函数声明悬挂在两个函数定义之间,特别是在使用它的函数之后.