1

I'm using OpenGL to render camera perspectives, and a one point in my code I'm trying to take the direction of the light (shown here as type "Vector4") and multiply it by a matrix of type "Matrix4x4" that represents the Modelview transformation (sorry if this is not making any sense, this is for a school project, as such I'm still learning about this stuff) Anyway, my code goes as follows...

Vector4 lightDirection = data->dir * follow->getModelviewMatrix().getInverse().getTranspose();
data->dir = lightDirection;
setLight(*data);

this give me the following error:

passing 'const vec4<double>' as 'this' argument of 'vec4<T>& vec4<T>::operator=(const vec4<T>&)[with T = double]' discards qualifiers

Again, much of this code is prewritten for the class (namely the vector and matrix types) but if someone could just help me decipher what the error means it would be much appreciated! I can give more information as needed.

I figured 'data' or 'data->dir' were const, however I can find no mention of either of them to be. 'dir' is of type SceneLightData, and when its added on I'm doing this:

void Scene::addLight(const SceneLightData &sceneLight)
{
    SceneLightData light = sceneLight;

    m_lights.push_back(&light);
}

The error occurs on this line:

data->dir = lightDirection;

EDIT

problem solved. thanks everyone! solution:

void Scene::addLight(const SceneLightData &sceneLight)
{
    SceneLightData* light = new SceneLightData;
    *light = sceneLight;

    m_lights.push_back(light);
}

and

SceneLightData* data = m_lights[i];

data->dir = data->dir * follow->getModelviewMatrix().getInverse().getTranspose();         
setLight(*data);
romeboards
  • 387
  • 5
  • 19
  • Is `Vector4` a `typedef` of `const vec4`? – Shahbaz Nov 12 '11 at 01:29
  • Oh wait, you get this error on the line that says `data->dir = lightDirection;`? – Shahbaz Nov 12 '11 at 01:30
  • `light` ceases to exist on exiting `addLight`, so the pointer you added to `m_lights` will be invalid on function return. You should be doing `m_lights.push_back(new SceneLightData(sceneLight));` and make sure it gets deleted when removed from `m_lights` or when `Scene` destructs. – Mike DeSimone Nov 12 '11 at 01:41
  • What is the type of `data`? Please provide the declaration of `data`, as well as the definition of whatever type it happens to be (or the definition of the type that is is a pointer to). – Mankarse Nov 12 '11 at 01:43

2 Answers2

1

data->dir is constant. It means you cannot change it, and you are trying to assign a different value to it, that's why compiler is getting mad at you. See const-correctness for more.

  • Thats what I thought, however I can find no mention of either dir or data to be const. 'dir' if of type SceneLightData, and when its added on I'm doing this: void Scene::addLight(const SceneLightData &sceneLight) { SceneLightData light = sceneLight; m_lights.push_back(&light); } – romeboards Nov 12 '11 at 01:32
  • @user991673: Your `dir` inside `data` will automatically be constant if `dir` itself is constant, which I believe is your case. –  Nov 12 '11 at 01:34
  • 1
    @user991673: O_o That code looks problematic. You are taking the address of a stack allocated object and then leaving the scope in which that object exists. The result of attempting to access `light` through that pointer at some later stage is undefined. – Mankarse Nov 12 '11 at 01:37
  • It's very unlikely that `dir` is `const`. You need to look at where `data` is declared. Is it a local variable? A parameter passed to the function? – Mike DeSimone Nov 12 '11 at 01:38
  • Yeah I'm not pleased about it. If I don't want the members of my m_lights vector to be const, what did I do if I'm given one? I've tried used const_cast but its never complied correctly with it... – romeboards Nov 12 '11 at 01:39
  • You can always `new` a non-const object, initializing it with a `const` object: `new SceneLightData(sceneLight)` – Mike DeSimone Nov 12 '11 at 01:43
  • `const_cast` works only on something that is not declared as `const` in the first place. Otherwise you have undefined behavior. To make member always non-const, you can declare it as `mutable`. Otherwise - just don't have that object as non-constant in the first place. If someone is passing you a constant object, the last thing they expect is that that object will be changed... So re-think your program design.. –  Nov 12 '11 at 01:45
  • solved the problem, check the original post for my fix (slash please let me know if its still horrid programming). Thanks everyone! – romeboards Nov 12 '11 at 01:46
0

This error occurs because you are attempting to call a non-const member function on a const object. At some point in your code you are calling the assignment operator of a const vec4<double>.

The assignment operator (operator=) is non-const because assigning to an object will modify the object that is being assigned to (to give it its new value).

It is not clear from your example where exactly the problem is occurring. If you are still having trouble, try reducing the example down to a minimal complete program which demonstrates the issue, and we can explain further.

Mankarse
  • 39,818
  • 11
  • 97
  • 141