g++我不认为我正在传递参考

发布于 2024-09-02 04:40:07 字数 544 浏览 6 评论 0原文

当我调用一个需要引用的方法时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澉约 2024-09-09 04:40:07

VertexInfo(n1index, n2index) 创建一个临时 VertexInfo 对象。临时对象不能绑定到非常量引用。

修改 addVertexInfo() 函数以采用常量引用可以解决此问题:

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

通常,如果函数不修改它通过引用采用的参数,则它应该采用常量引用。

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.

泛滥成性 2024-09-09 04:40:07

您可以将临时对象作为非常量引用传递。如果您无法更改 addVertexInfo 的签名,则需要在堆栈上创建您的信息:

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

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);
鹊巢 2024-09-09 04:40:07

VertexInfo &vi 更改为 VertexInfo const& vi

change VertexInfo &vi to VertexInfo const& vi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文