Why can't I call a non-const member function on an element in an std::set?

梦想的初衷 提交于 2019-12-11 06:58:14

问题


I hve the next code:

set<Item> temp_items;
set < Item >::iterator it;
temp_items = user->second.get_items();
for (it = temp_items.begin(); it != temp_items.end(); it++)
{
    if (counter == (choice - 1))
    {
        it->get_count();
    }
}

The item function i trying to call is:

int Item::get_count() { return _count; }

I don't have in here any const type that should prevent me from accessing the item object, and still, I get the next message:

the object has type qualifiers that are not compatible with the member function

object type is const

How can I fix it?


回答1:


In short: you can only call const functions on set iterators.

Explanation:

The set is internally sorted by the Item. So when you access a reference (via iterator) you are not allowed to do something to change the value of the Item (this was actually easily possible in earlier c++ versions).

What happens when you accidentally change the value this would not change the internal "ordering" accordingly and the lookup can fail even while the object is "there" in theory.

Imagine this with numbers you have a set

1 2 3 4 5 6 7 8 9

Now you change the value of 9 to 0 (by reference)

1 2 3 4 5 6 7 8 0

Now when you would look for a 0 and the container has a smart lookup it would use a binary search and would assume 0 is on the far left. This would result in 0 not being found.

(Note: this is just a simplified example. Not a definite explanation on how a set is implemented)

That is why the reference to the key is a const reference and why you only can call const functions on those (same applies for keys on a map).




回答2:


A set always gives you back objects that are const. In your loop, *it refers to a const Item. You're trying to call a member function, get_count(), that isn't const qualified - it is only allowed be called on non-const objects, hence the failure.

You can fix this by marking your function const:

int get_count() const { return _count; }
               ^^^^^^^

Note: you're calling a function that has no side-effects and not using its return type. This doesn't actually do anything.



来源:https://stackoverflow.com/questions/39791941/why-cant-i-call-a-non-const-member-function-on-an-element-in-an-stdset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!