Possible Duplicate:
C++ STL set update is tedious: I can't change an element in place
I want to use a std::set<>
to count the number occurences of a certain value and simultaneosly sort the objects. For this I created a class RadiusCounter
class RadiusCounter
{
public:
RadiusCounter(const ullong& ir) : r(ir) { counter = 1ULL; }
void inc() { ++counter; }
ullong get() const { return counter;}
ullong getR() const { return r;}
virtual ~RadiusCounter();
protected:
private:
ullong r;
ullong counter;
};
(the destructor does nothing) together with comparison operators:
const inline bool operator==(const RadiusCounter& a, const RadiusCounter& b) {return a.getR() == b.getR();}
const inline bool operator< (const RadiusCounter& a, const RadiusCounter& b) {return a.getR() < b.getR();}
const inline bool operator> (const RadiusCounter& a, const RadiusCounter& b) {return a.getR() > b.getR();}
const inline bool operator!=(const RadiusCounter& a, const RadiusCounter& b) {return a.getR() != b.getR();}
const inline bool operator<=(const RadiusCounter& a, const RadiusCounter& b) {return a.getR() <= b.getR();}
const inline bool operator>=(const RadiusCounter& a, const RadiusCounter& b) {return a.getR() >= b.getR();}
now I want to use it like this:
set<RadiusCounter> theRadii;
....
ullong r = getSomeValue();
RadiusCounter ctr(r);
set<RadiusCounter>::iterator itr = theRadii.find(ctr);
// new value -> insert
if (itr == theRadii.end()) theRadii.insert(ctr);
// existing value -> increase counter
else itr->inc();
But now the compiler complains at the line with the call to itr->inc()
:
error: passing 'const RadiusCounter' as 'this' argument of 'void RadiusCounter::inc()' discards qualifiers
Why is the instance in *itr
a const here?