Const 放弃资格赛:C++
我正在使用 OpenGL 渲染相机视角,在我的代码中,我尝试获取光线的方向(此处显示为“Vector4”类型)并将其乘以代表“Matrix4x4”类型的矩阵模型视图转换(抱歉,如果这没有任何意义,这是一个学校项目,因此我仍在学习这些东西)无论如何,我的代码如下......
Vector4 lightDirection = data->dir * follow->getModelviewMatrix().getInverse().getTranspose();
data->dir = lightDirection;
setLight(*data);
这给了我以下错误:
passing 'const vec4<double>' as 'this' argument of 'vec4<T>& vec4<T>::operator=(const vec4<T>&)[with T = double]' discards qualifiers
再次,大部分代码都是预先编写的对于该类(即向量和矩阵类型),但如果有人可以帮助我破译错误的含义,我将不胜感激!我可以根据需要提供更多信息。
我认为“data”或“data->dir”是常量,但是我发现没有提及它们中的任何一个。 “dir”的类型为 SceneLightData,当添加它时,我正在执行以下操作:
void Scene::addLight(const SceneLightData &sceneLight)
{
SceneLightData light = sceneLight;
m_lights.push_back(&light);
}
错误发生在这一行:
data->dir = lightDirection;
编辑
问题已解决。谢谢大家!解决方案:
void Scene::addLight(const SceneLightData &sceneLight)
{
SceneLightData* light = new SceneLightData;
*light = sceneLight;
m_lights.push_back(light);
}
和
SceneLightData* data = m_lights[i];
data->dir = data->dir * follow->getModelviewMatrix().getInverse().getTranspose();
setLight(*data);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
data->dir
是常量。这意味着您无法更改它,并且您正在尝试为其分配不同的值,这就是编译器对您生气的原因。有关更多信息,请参阅 const- Correctness。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.发生此错误的原因是您尝试在 const 对象上调用非常量成员函数。在代码中的某个时刻,您正在调用
const vec4
的赋值运算符。赋值运算符 (
operator=
) 是非常量运算符,因为对对象进行赋值将会修改被赋值的对象(为其赋予新值)。从您的示例中尚不清楚问题到底发生在哪里。如果您仍然遇到问题,请尝试将示例缩减为演示该问题的最小完整程序,我们可以进一步解释。
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.