没有上下文类型信息的重载函数 |无法解析重载函数“swap”基于到类型“int”的转换;
我正在尝试编写自己的冒泡排序算法作为练习。我不明白这两个错误消息。谁能指出我的代码有问题吗?
// 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 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到您正在使用
So 当您键入时
编译器无法消除您是指的是 std::swap 函数还是 int swap 变量的歧义。事实上,它看起来像是假设您正在引用该函数并尝试以某种方式将其转换为
int
类型。尝试将变量重命名为其他名称。一般来说,尽量远离
using
指令,以避免像这样的名称冲突。I see that you are using
So when you type
The compiler cannot disambiguate whether you are referring to the
std::swap
function or yourint swap
variable. In fact it looks like it assumed you were referring to the function and tried to somehow convert it to typeint
. Try renaming your variable to something else.In general, try to stay away from
using
directives, to avoid name collisions like this.这条线引起了问题。最好更改
swap
局部变量的名称,因为在std
命名空间中已经存在一个同名的函数,该函数由行引入作用域无论如何,应该避免使用命名空间 std;
。我还建议您在 if 块内声明变量,实际使用它:
最佳实践:通过延迟局部变量的声明来减少局部变量的范围,这意味着在实际使用它们的地方声明它们。不要在函数的开头声明它们。
解决代码中问题的另一种方法是为编译器提供一个上下文,您可以通过这样做(虽然我不会建议这个解决方案;这只是为了让您知道< /em>):
当你将
swap
转换为int
时,编译器可以知道swap
指的是局部变量,而不是函数。在std
命名空间中定义。This line is causing problem. It is better to change the name of
swap
local variable, as there exists already a function with same name, instd
namespace which is brought into scope by the lineusing namespace std;
which is to be avoided, anyway.I would also suggest you to declare the variable, inside the if-block where it is actually used:
Best practice: reduce the scope local variables by delaying their declarations, which means declare them where they are actually used. Do not declare them in the beginning of the function.
Another way to fix the problem in your code is to give the compiler a context which you can by doing this (though I wouldn't suggest this solution; it is just for you to know):
When you cast
swap
toint
, the compiler can know thatswap
refers to the local variable, not the function which is defined instd
namespace.该函数的返回类型是
void
。没有什么可打印的。如果需要打印排序后的数组,则需要在函数调用后迭代数组元素。The return type of the function is
void
. There is nothing to print for. If you need to print the sorted array, you need to iterate over the array elements after the function call.