“float”赋值中的不兼容类型“漂浮[16]”

发布于 2024-11-29 07:37:59 字数 422 浏览 3 评论 0原文

我有结构:

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 技术交流群。

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

发布评论

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

评论(5

哆兒滾 2024-12-06 07:37:59

getFloat() 返回一个指针。 m 是一个数组。如果您想将返回的所有内容复制到 m 中,则需要使用 std::copystd::memcpy

std::memcpy(m, r.getFloat(), sizeof(m));

如果您意味着要从 getFloat() 获取一个浮点数,您可以这样做:

  • m[0] = *r.getFloat();

getFloat() returns a pointer. m is an array. If you want to copy all of what is returned into m you will need to use std::copy or std::memcpy:

std::memcpy(m, r.getFloat(), sizeof(m));

If you meant to get just one float from getFloat() you could do:

  • m[0] = *r.getFloat();
昔梦 2024-12-06 07:37:59

r.getFloat() 返回一个指向单个浮点数的指针。对其进行解引用以给出单个浮点,然后分配给 16 个浮点的数组。

假设 m 包含 mat4 的整个状态,您可以使用内置的赋值运算符:

*this = r;

编译器将自动实现 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 of mat4, you can use the built in assignment operator:

*this = r;

The compiler will automatically implement the struct dump/ memcpy to copy all 16 floats.

绿光 2024-12-06 07:37:59

不要使用裸 C 数组。这是 C++,我们可以做得更好:

#include <tr1/array>  // or <array> if you're in MSVC or GCC

typedef std::tr1::array<float, 16> myfloats;

int main()
{
  myfloats a, b;
  a[0] = /* ... fill the array */
  b = a; // done
}

您还可以将数组放入矩阵结构中:

struct mat4
{
  typedef std::tr1::array<float, 16> myfloats;

  myfloats & data() { return m_data; }

  mat4(); /* ... etc.  ... */

private:
   myfloats m_data;
};

您应该能够直接将 mat4 类型的变量相互赋值!

Don't use naked C arrays. This is C++, we can do a lot better:

#include <tr1/array>  // or <array> if you're in MSVC or GCC

typedef std::tr1::array<float, 16> myfloats;

int main()
{
  myfloats a, b;
  a[0] = /* ... fill the array */
  b = a; // done
}

You can also put the array into your matrix structure:

struct mat4
{
  typedef std::tr1::array<float, 16> myfloats;

  myfloats & data() { return m_data; }

  mat4(); /* ... etc.  ... */

private:
   myfloats m_data;
};

You should be able to just assign variables of type mat4 to each other directly!

千里故人稀 2024-12-06 07:37:59

使用std::copy as:

#include <algorithm>

std::copy(r.m, r.m+16, m);

另外,如果你可以直接使用rm,为什么还要调用r.getFloat()呢?

Use std::copy as:

#include <algorithm>

std::copy(r.m, r.m+16, m);

Also, if you can use r.m directly, why call r.getFloat() then?

獨角戲 2024-12-06 07:37:59

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 with memcpy() or memmove() or with a for loop over the array elements.

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