w=1;
while(w<=nMaxTrains)
{//找到此时的第一个空位置(还末安排列车)w
     while(sectionTrains.GetAt(w-1).nSign!=0)
      {
w++;
if(w>nMaxTrains) break;
      }
     if(oRefLoops.GetAt(v).nSign==1) break;
     sectionTrains.GetAt(w-1).nLoop=oRefLoops.GetAt(v).nLoopId;//此行有错误
     sectionTrains[w-1].nSign=1;
     nLoopTrains++;
     if(nLoopTrains>=oRefLoops[v].nTrains) oRefLoops[v].nSign=1;
     w+=nSpace;
}
编译时出现:error C2106: '=' : left operand must be l-value
其中是sectionTrains是结构体数组:CArray<tagStruct,tagStruct> sections;
我把sectionTrains.GetAt(w-1).nLoop=oRefLoops.GetAt(v).nLoopId改成
sectionTrains[w-1].nLoop=oRefLoops.GetAt(v).nLoopId
编译时就顺利通过了。
这是为什么????在前第五行while条件中还用了sectionTrains.GetAt(w-1).nSign,为什么这里就不行了呢?我试过了,在出错的那一行用这样的语句:sectionTrains.GetAt(w-1).nSign=0也是一样的错误。
困惑中。。
请多指教。

解决方案 »

  1.   

    这与CArray::GetAt定义有关系吧!再好好看看MSDN吧!
      

  2.   

    GetAt返回的是tagStruct而不是tagStruct&,所以这个是不可以的……这个你干脆用sectionTrains.SetAt(w-1, oRefLoops.GetAt(v).nLoopId);好了
      

  3.   

    sectionTrains.GetAt(w-1).nLoop中nloop是什么类型?不会是一个const值吧?
      

  4.   

    to xing_xing_xing(未名):nLoop是int型,不是const(int nLoop;)
    to newbiestar():我有点明白了,GetAt()是“读取”的意思,返回的应该是一个类似“常量”,是右值,不能做左值。但你说的GetAt()返回的是tagStruct而不是&tagStruct,实际上我定义的也是tagStruct(CArray<tagStruct,tagStruct> sections)而不是&tagStruct(CArray<tagStruct,&tagStruct>.
    多谢了。
      

  5.   

    MSDN上定义:
    TYPE GetAt( int nIndex ) const;
    是const类型,返回值只能是右值。
    多谢