G ++ doesn't think I'm passing a link

When I call a method that accepts a link, g ++ complains that I am not passing a link. I thought the caller did not need to do anything for PBR. Here's the abusive code:

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

//method call:
sharedVertices[index]->addVertexInfo(VertexInfo(n1index, n2index));

And here is 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&)

+3
source share
3 answers

VertexInfo(n1index, n2index)creates a temporary object VertexInfo. Temporary cannot be attached to a non-constant link.

Changing your function addVertexInfo()to get a reference to const will help fix this problem:

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

In general, if a function does not change the argument that it takes by reference, it must accept a constant reference.

+12
source

. addVertexInfo, :

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

change VertexInfo &vitoVertexInfo const& vi

+1
source

Source: https://habr.com/ru/post/1746056/


All Articles