我正在尝试将自己的冒泡算法编写为练习.我不明白这两个错误消息.谁能用我的代码指出问题?
// Bubble sort algorithm
#include <iostream>
#include <iomanip>
using namespace std;
void bubbleSort(int array[], int arraySize); // bubbleSort prototype
int main(void)
{
const int arraySize = 10;
int array[arraySize] = {2,3,6,5,7,8,9,3,7,4};
cout << "Unsorted: ";
for(int i = 0; i < arraySize; ++i)
cout << setw(5) << array[i];
cout << "Sorted: " << bubbleSort(array, arraySize);
}
void bubbleSort(int array[], int arraySize)
{
const int max = arraySize;
int swap = 0;
for(int i = 0; i < max; ++i)
{
if(array[i] > array[i + 1])
{
swap = array[i + 1];
array[i + 1] = array[i];
array[i] = swap;
}
else
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我看到你正在使用
using namespace std;
Run Code Online (Sandbox Code Playgroud)
所以当你打字
array[i] = swap;
Run Code Online (Sandbox Code Playgroud)
编译器无法消除您是指代std::swap函数还是int swap变量的歧义.事实上,它看起来像是假设你指的是该函数并试图以某种方式将其转换为类型int.尝试将变量重命名为其他内容.
一般来说,尽量远离using指令,以避免像这样的名称冲突.