我正在尝试编写自己的气泡排序算法作为练习。我不明白这两个错误消息。谁能指出我的代码有问题吗?
I am trying to write my own bubble sort algorithm as an exercise. I do not understand the two error messages. Can anyone point out the problem with my code?
// 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; } }推荐答案
I看到您正在使用
using namespace std;所以当您键入
array[i] = swap;编译器无法消除您是否在引用 std :: swap 函数或您的 int交换变量。实际上,它似乎假定您正在引用该函数,并试图以某种方式将其转换为 int 类型。尝试将变量重命名为其他名称。
The compiler cannot disambiguate whether you are referring to the std::swap function or your int swap variable. In fact it looks like it assumed you were referring to the function and tried to somehow convert it to type int. Try renaming your variable to something else.
通常,请尝试使用指令远离以避免名称这样的碰撞。
In general, try to stay away from using directives, to avoid name collisions like this.
更多推荐
没有上下文类型信息的重载函数无法根据转换为“ int”类型来解析重载函数“ swap”
发布评论