Const 放弃资格赛:C++

发布于 2024-12-15 05:54:29 字数 1381 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-12-22 05:54:30

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.

鹿港小镇 2024-12-22 05:54:30

发生此错误的原因是您尝试在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文