Reputation: 119
Consider the following code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Vertex {
public:
int id;
vector<Vertex*> edges;
Vertex(int id) : id(id) {}
int get_id() const {return id;}
};
class Graph {
public:
vector<Vertex*> vertices;
Graph(int V) {
vertices.reserve(V);
}
void test(vector<Vertex*>& other) {
sort(vertices.begin(), vertices.end(), [](const Vertex*& one, const Vertex*& two) {return &*one < &*two;});
sort(other.begin(), other.end(), [](const Vertex*& one, const Vertex*& two) {return &*one < &*two;});
}
};
When I try to compile the above I get the error: error: no matching function for call to object of type '(lambda at Graph.cpp:59:48)' if (__comp(*--__last, *__first))
. I don't understand how I can fix this issue.
Upvotes: 0
Views: 53
Reputation: 96053
The parameters of your comparators are non-const references. More specifically, "references to non-const pointers to const Vertex
".
You either need const references: const Vertex *const &one
, or even better, just pass by value: const Vertex *one
.
Also note that &*one < &*two
is equivalent to one < two
.
Upvotes: 3