BadCash BadCash - 1 year ago 338
C++ Question

sort() - 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).


#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);


#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 Source

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á.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download