views:

97

answers:

3

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&)

+12  A: 

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.

James McNellis
Some documentation, of sorts: http://msdn.microsoft.com/en-us/library/cfbk5ddc%28VS.80%29.aspx
luke
+1  A: 

change VertexInfo &vi to VertexInfo const& vi

a1ex07
+3  A: 

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);
JSBangs