Reputation: 445
I have an std::set<MyType>
container which I'm getting by get function of some class, i.e. I have somethig like this:
typedef std::set<MyType, compareMyType> MyTypeSet; // this a singleton class class MyClass { ... public: MyTypeSet& getMySet() { return mySet_; } ... private: MyTypeSet mySet_; ... }
Then in other place I'm using this container:
MyClass obj;
MyTypeSet& mySet = mtyObj->getMySet();
MyTypeSet::iterator itBeg = mySet.begin();
// Then I'm trying to call f function
f(*itBeg);
Where f function has signature like below:
void f(MyType& param);
I'm getting compilation error like this:
note: candidates are: void f(MyType&)
error: no matching function for call to f(const MyType&)
How can I fix this?
Thanks in advance.
Upvotes: 3
Views: 152
Reputation: 4490
Without knowing details about your environment, I'm guessing your system's implementation of set
attempts to be "safe" by aliasing iterator
to be const_iterator
(so that you don't try to modify set items via the iterator, which is dangerous because it might change the sort order). See here for more about this.
If so, then the easiest fix is just to change the signature of your function to be void f(const MyType& param)
as the compiler suggests. If you do really need to modify the parameter (in such a way that it will not change the sort order), there are a variety of hacks to get around the const
restriction (including using const_cast
or the solution suggested at the above link), but I would recommend proceeding with caution.
Upvotes: 2