请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

no instance of overloaded function "swap" matches the argument list

我在#MinGW64#VSCode中会报swap的Error: no instance of overloaded function "swap" matches the argument list -- argument types are: (int, int). 求大神解析~

代码如下:

   int *generateNearlyOrderedArray(int n, int swapTimes){

       int *arr = new int[n];

       for(int i = 0 ; i < n ; i ++ )

           arr[i] = i;

       srand(time(NULL));

       for( int i = 0 ; i < swapTimes ; i ++ ){

           int posx = rand()%n;

           int posy = rand()%n;

           swap( arr[posx] , arr[posy] );

       }

       return arr;

   }

另外在SortTestHelper.h文件中为何有两次#include <string>呢?有什么区别呢?

正在回答 回答被采纳积分+3

1回答

liuyubobobo 2018-03-14 16:12:30

我不确定你的编译器版本。


首先确认自己有using namespace std; 否则请使用:std::swap(arr[posx] , arr[posy])


如果还不可以,尝试include<algorithm>

如果是C++11或以上版本,尝试include<utility>


课程官方代码:https://github.com/liuyubobobo/Play-with-Algorithms 

0 回复 有任何疑惑可以回复我~
  • 提问者 慕设计0065995 #1
    谢谢bobo老师!回复好快~我安装的是mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1,已经尝试了如下代码,仍然无效,另外如何查看自己安装的C++版本呢?我在另一台机器上安装了VS就正常,这台只是安装了VSCode。
    #include <iostream>
    #include <ctime>
    #include <cassert>
    #include <algorithm>
    #include <string>
    #include <utility>
    
    using namespace std;
    回复 有任何疑惑可以回复我~ 2018-03-14 16:32:39
  • 提问者 慕设计0065995 #2
    我在同namespace下写selectionSort函数,其中也有swap()就没有这个error.
    回复 有任何疑惑可以回复我~ 2018-03-14 16:58:11
  • liuyubobobo 回复 提问者 慕设计0065995 #3
    不知道为什么,我还是觉得你的C++配置有问题。确定你的代码里没有另外声明一个自定义的swap?试试强行用std::swap?
    回复 有任何疑惑可以回复我~ 2018-03-14 17:21:08
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号