嗨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& …Run Code Online (Sandbox Code Playgroud) 在下面的按值调用示例中,我无法理解为什么此代码不会将5的值更改为6.
第11行调用函数changeValue,其值为6,所以我认为应该输出6,但仍然输出5?
#include <iostream>
using namespace std;
void changeValue(int value);
int main()
{
int value = 5;
changeValue(value);
cout << "The value is : " << value << "." << endl;
return 0;
}
void changeValue(int value)
{
value = 6;
}
// This doesn't change the value from 5 to 6. 5 is output?
Run Code Online (Sandbox Code Playgroud)