sort()-没有匹配的函数可以调用'swap' [英] sort() - No matching function for call to 'swap'

查看:252
本文介绍了sort()-没有匹配的函数可以调用'swap'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了大约一个小时,试图弄清楚为什么我尝试构建以下类(在XCode中)时会收到20个类型为"Semantic issue - no matching function for call to 'swap'"的错误消息.

Just spent about an hour trying to figure out why I would get 20 error messages of the type "Semantic issue - no matching function for call to 'swap'" when I try to build the following class (in XCode).

test.h

#include <iostream>
#include <string>
#include <vector>


class Test{
    std::vector<std::string> list;

    void run() const;
    static bool algo(const std::string &str1, const std::string &str2);
};

test.cpp

#include "test.h"


void Test::run() const {
    std::sort( list.begin(), list.end(), algo );
}


bool Test::algo(const std::string &str1, const std::string &str2){
    // Compare and return bool
}

大多数有相同问题的人似乎都将他们的算法设为类成员而不是静态成员,但这显然不是问题所在.

Most of the people with the same problem seem to have made their algorithm a class member instead of a static member, but that is clearly not the problem here.

推荐答案

事实证明这是一个非常简单的问题,但发现起来并不十分明显(错误消息在帮助方面也不是一件好事) :

It turns out it's a very simple problem, but not very obvious to spot (and the error message doesn't do a very good job in helping out either):

删除run()-voilá上的const声明.

Remove the const declaration on run() - voilá.

这篇关于sort()-没有匹配的函数可以调用'swap'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆