Error "passing const xxx as 'this' argument discards qualifiers" in C++: how to fix
Hi! I'm a beginner C++ developer, and I've encountered an error I can't understand. I'm trying to use std::set to store custom objects, but the compiler gives me an error:
error: passing 'const MyClass' as 'this' argument of 'bool MyClass::operator<(const MyClass&)' discards qualifiers [-fpermissive]
Here's my code:
#include <iostream>
#include <set>
class MyClass {
public:
MyClass(int val) : value(val) {}
bool operator<(const MyClass& other) {
return this->value < other.value;
}
int getValue() const { return value; }
private:
int value;
};
int main() {
std::set<MyClass> mySet;
mySet.insert(MyClass(10));
mySet.insert(MyClass(5)); No answers yet.
Log in to leave an answer
Blogs