Re: c++ problem with temporany reference
On 11/22/2013 8:49 AM, alessio211734 wrote:
I have the following problem. I have this method:
void holeFilling::updateNearFaces( tri::Allocator<CMeshO>::PointerUpdater<CMeshO::FacePointer> pu, std::set<CMeshO::FacePointer> & faces )
{
std::set<CMeshO::FacePointer>::iterator it;
for (it=faces.begin();it!=faces.end();++it)
{
//CMeshO::FacePointer fp=(*it);
if (pu.NeedUpdate()) pu.Update((*it));
}
}
where the function Update is defined as:
template<class SimplexPointerType>
class PointerUpdater
{
void Update(SimplexPointerType &vp)
{
//if(vp>=newBase && vp<newEnd) return;
if(vp<oldBase || vp>oldEnd) return;
assert(vp>=oldBase);
assert(vp<oldEnd);
vp=newBase+(vp-oldBase);
if(!remap.empty())
vp = newBase + remap[vp-newBase];
}
with the microsoft compiler with no problem while with mingw I get an error.
...holeFilling.cpp:1471: error: no matching function for call to 'vcg::tri::Allocator<CMeshO>::PointerUpdater<CFaceO*>::Update(CFaceO* const&)'
if (pu.NeedUpdate()) pu.Update((*it));
^
if I replace the code so:
CMeshO::FacePointer fp=(*it);
if (pu.NeedUpdate()) pu.Update(fp);
compile on mingw but its different because I need to change the pointer (*it).
Your problem is that you're trying to update the value in 'std::set' in
place. It is not allowed. The set keeps its values sorted, so a change
in any value can disrupt the sorting. What you need to do is to
determine the new value and re-insert it into the set after removing the
one that doesn't need to be there anymore.
Technically, the set iterator returns a reference to a const value when
you dereference it. Microsoft cuts some corners (disable language
extensions to prohibit it from cutting corners), and that's why it
appears to work. In fact, your code has undefined behavior.
Change your loop
> for (it=faces.begin();it!=faces.end();++it)
> {
> //CMeshO::FacePointer fp=(*it);
> if (pu.NeedUpdate()) pu.Update((*it));
> }
to something like this
if (pu.NeedUpdate())
{
std::vector<CMeshO::FacePointer> updated;
for (it = faces.begin(); it != faces.end();) // note no ++it
{
CMeshO::FacePointer fp = *it;
pu.Update(fp);
updated.push_back(fp);
it = faces.erase(it); // this is where 'it' changes
}
for (int i = 0; i < updated.size(); ++i)
faces.insert(updated[i]);
}
V
--
I do not respond to top-posted replies, please don't ask