“float”赋值中的不兼容类型“漂浮[16]”
我有结构:
struct mat4 {
float m[16];
mat4();
...
float *getFloat();
}
float *mat4::getFloat() {
return m;
}
现在我想使 m 等于新创建的矩阵 r 中的 m:
void mat4::rotate(vec3 v) {
mat4 r, rx, ry, rz;
...
matrix calculations
...
m = *r.getFloat();
}
但这给了我错误“将‘float’分配给‘float [16]’时出现不兼容的类型” 我用谷歌搜索并尝试了不同的方法,但到目前为止没有成功。 请告诉我我该怎么做?
I have struct:
struct mat4 {
float m[16];
mat4();
...
float *getFloat();
}
float *mat4::getFloat() {
return m;
}
Now I want to make m equal to m from newly created matrix r:
void mat4::rotate(vec3 v) {
mat4 r, rx, ry, rz;
...
matrix calculations
...
m = *r.getFloat();
}
But this gives me error "incompatible types in assignment of ‘float’ to ‘float [16]’"
I have searched Google and tried different ways but no success so far.
Please tell me how could I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
getFloat()
返回一个指针。m
是一个数组。如果您想将返回的所有内容复制到m
中,则需要使用std::copy
或std::memcpy
:如果您意味着要从
getFloat()
获取一个浮点数,您可以这样做:m[0] = *r.getFloat();
getFloat()
returns a pointer.m
is an array. If you want to copy all of what is returned intom
you will need to usestd::copy
orstd::memcpy
:If you meant to get just one float from
getFloat()
you could do:m[0] = *r.getFloat();
r.getFloat() 返回一个指向单个浮点数的指针。对其进行解引用以给出单个浮点,然后分配给 16 个浮点的数组。
假设
m
包含mat4
的整个状态,您可以使用内置的赋值运算符:编译器将自动实现 struct dump/ memcpy 来复制所有 16 个浮点数。
r.getFloat()
is returning a pointer to a single float. This is dereferenced to give a single float, and then assigned to an array of 16 floats.Assuming that
m
contains the entire state ofmat4
, you can use the built in assignment operator:The compiler will automatically implement the struct dump/ memcpy to copy all 16 floats.
不要使用裸 C 数组。这是 C++,我们可以做得更好:
您还可以将数组放入矩阵结构中:
您应该能够直接将
mat4
类型的变量相互赋值!Don't use naked C arrays. This is C++, we can do a lot better:
You can also put the array into your matrix structure:
You should be able to just assign variables of type
mat4
to each other directly!使用
std::copy
as:另外,如果你可以直接使用
rm
,为什么还要调用r.getFloat()
呢?Use
std::copy
as:Also, if you can use
r.m
directly, why callr.getFloat()
then?r.getFloat() 返回一个指向“its”
m
的指针。数组不能通过简单的赋值来复制;您必须使用memcpy()
或memmove()
或对数组元素使用for
循环。r.getFloat() returns a pointer to "its"
m
. Arrays cannot be copied with simple assignment; you would have to work either withmemcpy()
ormemmove()
or with afor
loop over the array elements.