开发者

g++ doesn't think I'm passing a reference

When I call a method that takes a reference, g++ complains that I'm not passing a reference. I thought that the caller didn't have to do anything different for PBR. Here's the offending code:

//method definition
void addVertexInfo(VertexInfo &vi){vertexInstances.push_back(vi);} 

//method call:
sharedVertices[index]->a开发者_JAVA百科ddVertexInfo(VertexInfo(n1index, n2index));

And here's the error:

GLUtils/GLMesh.cpp: In member function 'void GLMesh::addPoly(GLIndexedPoly&)': GLUtils/GLMesh.cpp:110: error: no matching function for call to 'SharedVertexInfo::addVertexInfo(VertexInfo)' GLUtils/GLMesh.h:93: note: candidates are: void SharedVertexInfo::addVertexInfo(VertexInfo&)


VertexInfo(n1index, n2index) creates a temporary VertexInfo object. A temporary cannot be bound to a non-const reference.

Modifying your addVertexInfo() function to take a const reference would fix this problem:

void addVertexInfo(const VertexInfo& vi) { /* ... */ }

In general, if a function does not modify an argument that it takes by reference, it should take a const reference.


You can's pass a temporary object as a non-const reference. If you can't change the signature of addVertexInfo, you need to create your info on the stack:

VertexInfo vi(n1index, n2index);
sharedVertices[index]->addVertexInfo(vi);


change VertexInfo &vi to VertexInfo const& vi

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜