g++我不认为我正在传递参考
当我调用一个需要引用的方法时,g++ 抱怨我没有传递引用。我认为调用者不必为 PBR 做任何不同的事情。这是有问题的代码:
//method definition
void addVertexInfo(VertexInfo &vi){vertexInstances.push_back(vi);}
//method call:
sharedVertices[index]->addVertexInfo(VertexInfo(n1index, n2index));
这是错误:
GLUtils/GLMesh.cpp:在成员函数“void GLMesh::addPoly(GLIndexedPoly&)”中: GLUtils/GLMesh.cpp:110:错误:没有匹配的函数可用于调用“SharedVertexInfo::addVertexInfo(VertexInfo)” GLUtils/GLMesh.h:93:注意:候选者是:void SharedVertexInfo::addVertexInfo(VertexInfo&)
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]->addVertexInfo(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&)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
VertexInfo(n1index, n2index)
创建一个临时VertexInfo
对象。临时对象不能绑定到非常量引用。修改
addVertexInfo()
函数以采用常量引用可以解决此问题:通常,如果函数不修改它通过引用采用的参数,则它应该采用常量引用。
VertexInfo(n1index, n2index)
creates a temporaryVertexInfo
object. A temporary cannot be bound to a non-const reference.Modifying your
addVertexInfo()
function to take a const reference would fix this problem:In general, if a function does not modify an argument that it takes by reference, it should take a const reference.
您可以将临时对象作为非常量引用传递。如果您无法更改
addVertexInfo
的签名,则需要在堆栈上创建您的信息: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
更改为VertexInfo const& vi
change
VertexInfo &vi
toVertexInfo const& vi