I have the following code:
class Test{
private:
int id;
public:
Test(int v):id(v) {}
int getId() { return id;};
int getId() const { return id;};
and all the errors gone
};
struct compare{
bool operator()(const Test& t1, const Test& t2){
return t1.getId() < t2.getId();
}
};
int main(int argc, char *argv[]){
set<Test, compare> s;
Test str[] = {Test(1), Test(2), Test(3)};
for (int i = 0; i < 3; ++i){
s.insert(str[i]);
}
for (set<Test>::iterator it = s.begin(); it != s.end(); ++it){
cout << it->getId() << "\n";
}
return EXIT_SUCCESS;
}
I got this error when I called the getId () method with this code:
passing `const Test' as `this' argument of `int Test::getId()' discards qualifiers
I don’t know why I need const in the getId () method to fix this error? Thanks
source
share