confusing behaviour with iterators .net 2003

  • Thread starter Thread starter Kavvy
  • Start date Start date
K

Kavvy

Hello,

Where it2 is a vector<Body3d>::const_iterator

How come I can't do this
float xd = (*it2).getXpos();

(Which fails with a "error C2662: 'Body3d::getXpos' : cannot convert 'this'
pointer from 'const std::allocator<_Ty>::value_type' to 'Body3d &')

I have to do this to get it to work.
Body3d bd = (*it2);
float xd = db.getXpos();

Seems like a step backwards!
 
Hello,

Where it2 is a vector<Body3d>::const_iterator

How come I can't do this
float xd = (*it2).getXpos();

(Which fails with a "error C2662: 'Body3d::getXpos' : cannot convert 'this'
pointer from 'const std::allocator<_Ty>::value_type' to 'Body3d &')

I have to do this to get it to work.
Body3d bd = (*it2);
float xd = db.getXpos();

Seems like a step backwards!

Ooops!

It needs to be an iterator rather than a constant iterator!
 
Kavvy said:
Hello,

Where it2 is a vector<Body3d>::const_iterator

How come I can't do this
float xd = (*it2).getXpos();

(Which fails with a "error C2662: 'Body3d::getXpos' : cannot convert
'this' pointer from 'const std::allocator<_Ty>::value_type' to
'Body3d &')

I have to do this to get it to work.
Body3d bd = (*it2);
float xd = db.getXpos();

Seems like a step backwards!

It's because Body3d::getXpos() isn't a const member, e.g.

class Body3d
{
// ...
float getXpos() const
{
return ...
}
};

-cd
 
Kavvy said:
Ooops!

It needs to be an iterator rather than a constant iterator!

A better solution would be to be to make your classes const-correct,
starting by changing getXpos() to be a const member function.
 
Back
Top