没有上下文类型信息的重载函数无法根据转换为“ int”类型来解析重载函数“ swap”

编程入门 行业动态 更新时间:2024-10-19 20:31:44
本文介绍了没有上下文类型信息的重载函数无法根据转换为“ int”类型来解析重载函数“ swap”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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”

本文发布于:2023-11-14 09:34:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   类型   上下文   转换为   信息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!