没有上下文类型信息的重载函数 |无法解析重载函数“swap”基于到类型“int”的转换;

发布于 2025-01-05 21:10:35 字数 1004 浏览 4 评论 0原文

我正在尝试编写自己的冒泡排序算法作为练习。我不明白这两个错误消息。谁能指出我的代码有问题吗?

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佼人 2025-01-12 21:10:35

我看到您正在使用

using namespace std;

So 当您键入时

array[i] = swap;

编译器无法消除您是指的是 std::swap 函数还是 int swap 变量的歧义。事实上,它看起来像是假设您正在引用该函数并尝试以某种方式将其转换为 int 类型。尝试将变量重命名为其他名称。

一般来说,尽量远离 using 指令,以避免像这样的名称冲突。

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.

In general, try to stay away from using directives, to avoid name collisions like this.

从来不烧饼 2025-01-12 21:10:35
array[i] = swap;

这条线引起了问题。最好更改 swap 局部变量的名称,因为在 std 命名空间中已经存在一个同名的函数,该函数由行 引入作用域无论如何,应该避免使用命名空间 std;

我还建议您在 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;
}

最佳实践:通过延迟局部变量的声明来减少局部变量的范围,这意味着在实际使用它们的地方声明它们。不要在函数的开头声明它们。

解决代码中问题的另一种方法是为编译器提供一个上下文,您可以通过这样做(虽然我不会建议这个解决方案;这只是为了让您知道< /em>):

array[i] = (int)swap; //giving compiler contextual type information

当你将 swap 转换为 int 时,编译器可以知道 swap 指的是局部变量,而不是函数。在 std 命名空间中定义。

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.

I would also suggest you to declare the variable, inside the if-block where it is actually used:

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 context which 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.

如日中天 2025-01-12 21:10:35
cout << "Sorted: " << bubbleSort(array, arraySize);

该函数的返回类型是void。没有什么可打印的。如果需要打印排序后的数组,则需要在函数调用后迭代数组元素。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文