Reputation: 553
so i am doing a inventory program. I have a vector of class items in it and I want to user enter Id and then run an iterator to match with the Id that is in the vector and change the value of the quantity sold:
here is part of my code:
vector<Item>::const_iterator it;
for(it=items.begin(); it !=items.end(); it++){
if (it->getID() == id){
amount=it->getactual()-sold;
it->setactual(amount);
that is in my class
int getactual()const{return actual_quantity;}
void setactual(int quantity){actual_quantity=quantity;}
but i get an error: passing ‘const Item’ as ‘this’ argument of ‘void Item::setactual(int)’ discards qualifiers
Upvotes: 0
Views: 89
Reputation: 45410
You should use
vector<Item>::iterator
instead of
vector<Item>::const_iterator
const_iterator
means read-only.
Upvotes: 1
Reputation: 126777
setactual
obviously cannot operate on a const
object (as the one that you obtain via a const_iterator
; change const_iterator
to plain iterator
.
Upvotes: 1
Reputation: 10348
The problem is that you are using a const iterator but you are modifying what it points to (calling non-const members), try using a regular iterator:
vector<Item>::iterator it;
Upvotes: 1