sort() - No matching function for call to 'swap'

Magnus W picture Magnus W · Feb 27, 2015 · Viewed 17.4k times · Source

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.

Answer

Magnus W picture Magnus W · Feb 27, 2015

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):

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