I have a class which has one private member:
std::set<unsigned long> Sset;
And I have a problem with this function:
Prototype:
Set& Union (Set&, Set&);
Code:
Set& Set::Union (Set& s1, Set& s2)
{
set<unsigned long>::iterator a;
set<unsigned long>::iterator j;
for (a = s1.Sset.begin(); a!=s1.Sset.end(); ++a)
for (j = s2.Sset.begin(); j!=s2.Sset.end(); ++j)
if (*a = *j)
{
Sset.insert(*a);
break;
}
return *this;
}
I get compiler error expression must be a modifiable lvalue at *a=*j
Everything is OK with iterator j, but it won’t accept *a
Any help, or explanation?
Thanks
You can’t assign to the value pointed to by a
set::iterator.Why?
Because sets keep their elements in whatever order it feels like (
set, specifically, is sorted) to allow for quick retrieval, so you can’t manually tell it where to put elements.Instead,
insertthe element at the right place.Having said that — did you mean to use
==instead of=?