I am learning C ++ and cannot solve this problem:
I have a simple class A
class A { private: int ival; float fval; public: A(int i = 0, float f = 0.0) : ival(i), fval(f) { } ~A(){ } void show() const { cout << ival << " : " << fval << "\n"; } void setVal(int i) { ival = i; }
Then I have a regular set<A> myset , which is populated with insert(A(2, 2.2)); in a loop.
An iteration to get all values โโis not a problem, but I want to change the value in this iteration:
for(set<A>::iterator iter = set3.begin(); iter != set3.end(); iter++) { iter->setVal(1); }
I suppose this should be doable, as you would do it in Java in a foreach loop. When compiling, I get error: passing 'const A' as 'this' argument of 'void A::setVal(int)' discards qualifiers .
Looking at the sources of the STL collection, I can see that begin() is only available as a const method, and I think this might be a problem. The mess with a constant in the setVal() method always got the same error and doesn't make much sense since I want to change the value of A
Is this the wrong approach to changing the set of values โโof A using a loop?
source share