c++ - sort() - 没有匹配函数来调用 'swap'

标签 c++ xcode sorting compiler-errors swap

我花了大约一个小时试图弄清楚为什么当我尝试构建以下内容时,我会收到 20 条类型为 “语义问题 - 没有匹配函数调用 'swap'” 的错误消息类(在 XCode 中)。

测试.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);
};

测试.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
}

大多数遇到同样问题的人似乎都将他们的算法设为类成员而不是静态成员,但这显然不是这里的问题。

最佳答案

事实证明这是一个非常简单的问题,但不是很明显(并且错误消息也不能很好地帮助解决):

删除 run() 上的 const 声明 - 瞧。

关于c++ - sort() - 没有匹配函数来调用 'swap',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28761802/

相关文章:

c++ - 随机时间延迟

c++ - 我如何在不使用“using namespace std; 的情况下使用 _getch()?

objective-c - AVPlayer 转发类声明

java - 自定义 Java 按名称排序

C++ 迭代器到 const_iterator

android - Android 上未加载共享库

ios - 用户选择选项卡时如何重置 UITabBarController 的选项卡(或选项卡 Controller )?

swift - 类型 'StorageMetadata' 的值没有成员 'downloadURL'

c - 链表中的 strncmp

JAVA:如何根据字段对列表进行排序