C++ 没有上下文类型信息的重载函数| 无法解析基于转换为“int”类型的重载函数“swap”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9304926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
overloaded function with no contextual type information | cannot resolve overloaded function 'swap' based on conversion to type 'int'
提问by Jonathan Dewein
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;
}
}
回答by Alexander Kondratskiy
I see that you are using
我看到你正在使用
using namespace std;
So when you type
所以当你输入
array[i] = swap;
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.
编译器无法区分您指的是std::swap
函数还是int swap
变量。事实上,它似乎假设您指的是该函数并试图以某种方式将其转换为 type int
。尝试将您的变量重命名为其他名称。
In general, try to stay away from using
directives, to avoid name collisions like this.
一般来说,尽量远离using
指令,以避免像这样的名称冲突。
回答by Nawaz
array[i] = swap;
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, in std
namespace which is brought into scope by the line using namespace std;
which is to be avoided, anyway.
这条线引起了问题。最好更改swap
局部变量的名称,因为在std
命名空间中已经存在一个具有相同名称的函数using namespace std;
,无论如何,该函数被要避免的行带入范围。
I would also suggest you to declare the variable, inside the if-block where it is actuallyused:
我还建议您在实际使用它的 if 块内声明变量:
if(array[i] > array[i + 1])
{
//declare temp here where it is actually used!
int temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
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 contextwhich you can by doing this (though I wouldn't suggest this solution; it is just for you to know):
解决代码中问题的另一种方法是为编译器提供一个上下文,您可以这样做(尽管我不建议使用此解决方案;它仅供您了解):
array[i] = (int)swap; //giving compiler contextual type information
When you cast swap
to int
, the compiler can know that swap
refers to the local variable, not the function which is defined in std
namespace.
当您强制转换swap
为 时int
,编译器可以知道它swap
指的是局部变量,而不是在std
命名空间中定义的函数。
回答by Mahesh
cout << "Sorted: " << bubbleSort(array, arraySize);
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.
函数的返回类型是void
。没有什么可打印的。如果需要打印排序后的数组,则需要在函数调用后遍历数组元素。