gpt4 book ai didi

c++ - 错误 : cannot convert 'float*' to 'qreal* {aka double*} in initialization

转载 作者:行者123 更新时间:2023-11-30 01:15:55 24 4
gpt4 key购买 nike

我在尝试编译旧的 Qt 项目时遇到了这个错误:

error: cannot convert 'float*' to 'qreal* {aka double*}' in initialization

这是代码片段:

void Camera::loadProjectionMatrix()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
qreal *dataMat = projectionMatrix_.data();
GLfloat matriceArray[16];
for (int i= 0; i < 16; ++i)
matriceArray[i] = dataMat[i];
glMultMatrixf(matriceArray);
}

我有什么选择来克服这个错误?

最佳答案

根据文档,投影矩阵将返回float*:

float * QMatrix4x4::​data()

Returns a pointer to the raw data of this matrix.

无论这种情况如何,最好的做法是在您的代码库中消除 qreal 的使用。当贡献者进行 Qt 5 重构时,qreal 古老的概念被尽可能多地丢弃,绝对不应该在 API 处理 float 的新代码中使用太多。

目前在这种情况下建议使用 float 。这有点历史,真的。那时,将 qreal 定义为在可用的地方加倍,但在不可用的地方定义为 float 是有意义的,例如ARM 平台。请参阅旧文档:

typedef qreal

Typedef for double on all platforms except for those using CPUs with ARM architectures. On ARM-based platforms, qreal is a typedef for float for performance reasons.

在 Qt 5 中,文档略有不同,但主要概念似乎保持不变:

typedef qreal

Typedef for double unless Qt is configured with the -qreal float option.

我会按照以下方式修复您的代码:

void Camera::loadProjectionMatrix()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float *dataMat = projectionMatrix_.data();
GLfloat matriceArray[16];
for (int i= 0; i < 16; ++i)
matriceArray[i] = dataMat[i];
glMultMatrixf(matriceArray);
}

严格来说,您也可以使用另一种方法来解决问题,即使用此方法而不是 data():

float & QMatrix4x4::​operator()(int row, int column)

Returns a reference to the element at position (row, column) in this matrix so that the element can be assigned to.

在这种情况下,您甚至可以消除 dataMat 变量,并在迭代中将项目直接分配给您的 matriceArray

更进一步,您应该考虑使用 Qt 库来完成这项常见任务,例如QtGuiQt3D 中的 opengl 类。如果你做一些自定义的事情,那么搞乱低级 opengl API 调用会更有意义。

关于c++ - 错误 : cannot convert 'float*' to 'qreal* {aka double*} in initialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com