2010-11-14 112 views
1

即時得到這兩個錯誤C++智能感知:表達式必須修改的左值

1>c:\users\owner\documents\visual studio 2010\projects\monopoly\monopoly\xfileentity.cpp(376): error C3490: 'pDrawMesh' cannot be modified because it is being accessed through a const object 
IntelliSense: expression must be a modifiable lvalue 

我宣佈pDrawMesh在我的課不是在一個函數中使用它。
這裏是我的類

class CXFileEntity 
{ 
     ...... 
LPD3DXMESH pDrawMesh; 
     ..... 
}; 

這裏是我使用的變量

void CXFileEntity::DrawMeshContainer(LPD3DXMESHCONTAINER meshContainerBase, LPD3DXFRAME frameBase) const 
{ 
// Cast to our extended frame type 
D3DXFRAME_EXTENDED *frame = (D3DXFRAME_EXTENDED*)frameBase; 

// Cast to our extended mesh container 
D3DXMESHCONTAINER_EXTENDED *meshContainer = (D3DXMESHCONTAINER_EXTENDED*)meshContainerBase; 

// Set the world transform But only if it is not a skinned mesh. 
// The skinned mesh has the transform built in (the vertices are already transformed into world space) so we set identity 
// Added 24/08/10 
if (meshContainer->pSkinInfo) 
{ 
    D3DXMATRIX mat; 
    D3DXMatrixIdentity(&mat); 
    m_d3dDevice->SetTransform(D3DTS_WORLD, &mat); 
} 
else 
    m_d3dDevice->SetTransform(D3DTS_WORLD, &frame->exCombinedTransformationMatrix); 


// Loop through all the materials in the mesh rendering each subset 
for (unsigned int iMaterial = 0; iMaterial < meshContainer->NumMaterials; iMaterial++) 
{ 
    // use the material in our extended data rather than the one in meshContainer->pMaterials[iMaterial].MatD3D 
    m_d3dDevice->SetMaterial(&meshContainer->exMaterials[iMaterial]); 
    m_d3dDevice->SetTexture(0, meshContainer->exTextures[iMaterial]); 

    // Select the mesh to draw, if there is skin then use the skinned mesh else the normal one 
    pDrawMesh = (meshContainer->pSkinInfo) ? meshContainer->exSkinMesh: meshContainer->MeshData.pMesh; 

    // Finally Call the mesh draw function 
    pDrawMesh->DrawSubset(iMaterial); 
} 
} 

回答

4

你的成員函數是const限定。除非聲明爲可變,否則不能修改來自const限定成員函數的任何成員變量。

您需要使pDrawMesh變爲可變,從DrawMeshContainer中刪除常規資格,或者找到其他方法來完成您嘗試完成的任何操作。

+0

毆打10秒...導致忘記關鍵字可變。 – Dialecticus 2010-11-14 23:58:03

0

pDrawMesh確實是this->pDrawMesh。但是由於目前的方法是const方法,因此thisconst CXFileEntity*。所以你不能設置成員pDrawMesh

如果DrawMeshContainer真的應該更改CXFileEntity,請從方法類型中刪除const。如果DrawMeshContainer「有效」保持CXFileEntity不變,並且pDrawMesh成員對於對象的「const」的含義「沒有真正計數」,則可以將成員更改爲mutable