Fehler "passing const xxx as 'this' argument discards qualifiers" in C++: wie beheben
Hallo! Ich bin ein Anfänger C++ Entwickler und bin auf einen Fehler gestoßen, den ich nicht verstehe. Ich versuche std::set zu verwenden, um benutzerdefinierte Objekte zu speichern, aber der Compiler gibt einen Fehler aus:
error: passing 'const MyClass' as 'this' argument of 'bool MyClass::operator<(const MyClass&)' discards qualifiers [-fpermissive]
Hier ist mein 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));