error C2662: CObject::getFacetIndicesAt' : cannot convert 'this' pointer from 'const hiveRayTracing::CObject' to 'hiveRayTracing::CO(c++ const this )

 

 1 class CObject
 2     {
 3         friend class CPlyLoader;
 4     public:
 5         CObject::CObject() : m_pMaterial(NULL) {}
 6         CObject::~CObject() 
 7         {
 8             m_Vertex.clear();
 9             m_VertexNormal.clear();
10             m_FacetIndices.clear();
11             m_FacetNormal.clear();
12         }
13 
14         void setMaterial(const CMaterial* pMaterial)  {m_pMaterial  = pMaterial;}
15 
16         Point3f getVertexAt(unsigned int vIndex)        {_ASSERT(vIndex < m_Vertex.size()); return m_Vertex.at(vIndex);}
22         Point3f getVertexNormalAt(unsigned int vIndex)  {_ASSERT(vIndex < m_VertexNormal.size()); return m_VertexNormal.at(vIndex);}
23         Point3i getFacetIndicesAt(unsigned int vIndex)  {_ASSERT(vIndex < m_FacetIndices.size()); return m_FacetIndices.at(vIndex);}
24         Point3f getFacetNormalAt(unsigned int vIndex)   {_ASSERT(vIndex < m_Vertex.size()); return m_FacetNormal.at(vIndex);}
25         
     
32         const CMaterial*  getMaterial()  const  {return m_pMaterial;}
33 
34     private:
35         std::vector<Point3f> m_Vertex;
36         std::vector<Point3f> m_VertexNormal;
37         std::vector<Point3i> m_FacetIndices;
38         std::vector<Point3f> m_FacetNormal;

 

class CScene
    {
    public:
        CScene();
        ~CScene();

        void loadModel(const std::string vFileName)
        {
            m_pObject = new CObject();
            m_Ply.load(vFileName, m_pObject);
        }
        const CObject* getObject() const          {return m_pObject;}     .............    }

 

然后我在一段代码中调用pObject->getFacetIndicesAt(vLeaf.getElementAt(i))就会出错

 1 void queryLeafNode(const TVertexProperty& vLeaf)
 2         {
 3             const CObject* pObject = getObject();//注意这里是 const*  4             CBBox BBox = vLeaf.getNodeInfo().BBox;
 5             float tMin, tMax;
 6             if (!BBox.intersectRay(m_CurrentQuery, tMin, tMax)) return;
 7             m_CurrentQuery.setMint(tMin);
 8             m_CurrentQuery.setMaxt(tMax);
 9             
10             unsigned int NumElement = vLeaf.getNumElements();
11             for (unsigned int i = 0; i < NumElement; ++i)
12             {
13                 Point3i Index = pObject->getFacetIndicesAt(vLeaf.getElementAt(i));
14                 float t, u, v;
15                 if (rayIntersectTriangle(pObject->getVertexAt(Index[0]), pObject->getVertexAt(Index[1]), pObject->getVertexAt(Index[2]), 16                                          m_CurrentQuery, u, v, t))
17                 {
18                     if (t > m_QueryResult.curDis) continue;
19 
20                     m_QueryResult.intersectPos = m_CurrentQuery.getOrigin() + m_CurrentQuery.getDir() * t;
21                     m_QueryResult.curDis = t; 
22                     m_QueryResult.u = u;
23                     m_QueryResult.v = v;
24                     m_QueryResult.triIdx = vLeaf.getElementAt(i);
25                 }
26             }
27         }

第15行 error C2662: 'hiveRayTracing::CObject::getFacetIndicesAt' : cannot convert 'this' pointer from 'const hiveRayTracing::CObject' to 'hiveRayTracing::CObject &'

原因在于带“后”const(成员函数后面:getX() const之类的)修饰符的接口会把this指针转化为为const this类型 ,但是getFacetIndicesAt()是非“后带”const修饰的接口。

修改方法:(1)Point3f getVertexAt(unsigned int vIndex)       const {_ASSERT(vIndex < m_Vertex.size()); return m_Vertex.at(vIndex);}

          (2)Point3f getVertexAt(unsigned int vIndex)       {_ASSERT(vIndex < m_Vertex.size()); return std::const_cast<Point3f*>(this)m_Vertex.at(vIndex);}

 

posted @ 2013-11-03 18:29  forgood  阅读(242)  评论(0编辑  收藏  举报