I’m having some issues getting the following code to compile:
GLint iModelMatrix = glGetUniformLocation(m_shaderProgramID, "mvMatrix");
glUniformMatrix4fv(iModelMatrix, 1, GL_FALSE, &myMatrix[0]);
GLint iLight = glGetUniformLocation(m_shaderProgramID, "vLightPos");
glUniform3fv(iLight, 1, &myVector[0]);
Now myMatrix and myVector are two objects, each of these classes have the [] operator overloaded as follows:
inline float& operator[](int index) { return m_data[index]; }
inline float operator[](int index) const { return m_data[index]; }
I receive the following error:
error C2102: '&' requires l-value
I’m trying to udnerstand why ‘&’ requires a left hand side value? I’ve passed data like this before. Arent I simply giving the address to the first index of the m_data array? Doesn’t that constitute as a float*?
Why does it not satisfy the function specification?
void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
EDIT:
alright it seems that because im passing these objects as const refereneces to the function that makes the above calls, it is passing the float* arrays by value, and thus i can’t reference them.
I tried adding a third [] operator:
inline const float& operator[](int index) const { return m_data[index]; }
But that made my compiler explode. Is there a reason why i cannot do this?
This:
should be converted into this:
if you want to take the address of the return value. You don’t add a third version; you change the current one into this. You can’t take the address of a temporary, but you can take the address of a
const&.Personally, I think you should have a specialzed function that just returns a
float*orconst float*, much like C++11 did with std::vector::data. That way, you just callmyMatrix.data()rather than having to do a&...[0]kludge. It’s self-documenting and obvious.