为何同一个类里面一处有错,另一处没有错?解决办法

为何同一个类里面一处有错,另一处没有错?在local_from_world()这个函数里面, for(iter shapes_.begin()

为何同一个类里面一处有错,另一处没有错?
在local_from_world()这个函数里面, for(iter = shapes_.begin(); iter!= shapes_.end(); iter++)这一行出现错误:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_const_iterator<_Ty,_Alloc>' (or there is no acceptable conversion)

但在render函数里面,同样这一行,却没错?

为啥?

代码如下

C/C++ code
Matrix3x3 CoordinateSystem::local_from_world() const    {    Matrix3x3 m;    m.assignIdentity();    std::vector<Shape*>::iterator iter;    for(iter = shapes_.begin(); iter!= shapes_.end(); iter++)    {    if ((*iter)->parent()!=NULL)        {        m=(*iter)->parent()->local_from_parent()*m;        }    }    return m;    }//在render函数里面却没有错?void CoordinateSystem::render()    {    // push and post multiply the OpenGL modelview matrix by a transformation that    // maps this coordinate system's local space into its parent space.    glMatrixMode(GL_MODELVIEW);    glPushMatrix();      Matrix3x3 mmd=parent_from_local();      Matrix3x3 nnd=mmd.transpose();       GLdouble m[16]={*(nnd[0]),*(nnd[0]+1),0,*(nnd[0]+2),*(nnd[1]),*(nnd[1]+1),0,*(nnd[1]+2),      0,0,1,0,       *(nnd[2]),*(nnd[2]+1),0,*(nnd[2]+2)};              glMultMatrixd(m);    std::vector<Shape*>::iterator iter;    for(iter = shapes_.begin(); iter!= shapes_.end(); iter++)    {    (*iter)->render();    }    std::vector<CoordinateSystem*>::iterator CS_iter;    for(CS_iter = children_.begin(); CS_iter != children_.end(); CS_iter++)    {    (*CS_iter)->render();    }    glPopMatrix();    }


[解决办法]
这个错误的原因是关键字const,先讲一下const,

1. const 返回类型 函数名(参数表) const; // 函数声名或定义。
2. 返回类型 函数名(参数表); // 

我们常见的用const修饰函数,见1,通常情况下,应该是这个类型,但有时候,返回值是一个简单值(int, bool, char等)时,返回类型前的const可以不要。参数表后面的const,修饰了这个函数的状态是一个常恹,它强制编译器检查函数内部没有修改和读取指针、对象等的操作。对于函数中的一个对象,自然以常对象来表示。

从上面的讲述中,我想你已经明白,为什么报错,在local_from_world()函数已经用const表示,所以其中的shapes_.begin(); 返回的,也是基于常对象的跌代器,你应该将std::vector<Shape*>::iterator iter;声明改为使用常跌代器的声明std::vector<Shape*>::const_iterator iter;
[解决办法]
探讨
这个错误的原因是关键字const,先讲一下const,

1. const 返回类型 函数名(参数表) const; // 函数声名或定义。
2. 返回类型 函数名(参数表); //

我们常见的用const修饰函数,见1,通常情况下,应该是这个类型,但有时候,返回值是一个简单值(int, bool, char等)时,返回类型前的const可以不要。参数表后面的const,修饰了这个函……