Ç - 无效使用非左值数组 [英] C - invalid use of non-lvalue array

查看:315
本文介绍了Ç - 无效使用非左值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵结构:

  typedef结构矩阵
{
    浮动米[16];
}矩阵;

当我尝试调用这个函数:

 的memcpy(M>男,MultiplyMatrices(男,&安培;译).M,的sizeof(M> M));

我在编译的时候得到一个错误说:


  

错误:无效使用非左值数组


MultiplyMatrices返回一个矩阵。

如果我使用gcc将文件编译成一个对象,我只得到这个错误,
如果我用G ++编译对象我没有得到任何错误。

我甚至不知道什么是错误意味着,我有一种感觉它与存储在由MultiplyMatrices返回的矩阵阵列做。

如果您需要查看更多code让我知道。

这code是从本教程: OpenGL的书第4章

P.S。我想保持这个code严格的ISO / ANSI,如果没有其他解决办法然而,那我只好对付它。

编辑:我结束了创建一个临时矩阵,然后复制阵列会

 矩阵tempMatrix;...tempMatrix = MultiplyMatrices(男,&安培;翻译);
的memcpy(M>男,tempMatrix.m,sizeof的(M> M));


的返回值MultiplyMatrices()不是一个左值(就像任何函数的返回值) ,这意味着你不能把它的地址。评估一个数组(包括结构数组成员)隐含需要的第一个元素的地址,所以你不能做到这一点。

您可以,但是,使用简单的赋值包含结构

  * M = MultiplyMatrices(男,&安培;翻译);

只要你的结构仅包含您所显示的一个元素,这是完全一样的。

I have a matrix struct:

typedef struct Matrix
{
    float m[16];
} Matrix;

When I try to call this function:

memcpy(m->m, MultiplyMatrices(m, &translation).m, sizeof(m->m));

I get an error at compile time saying:

error: invalid use of non-lvalue array

MultiplyMatrices returns a Matrix.

I only get this error if I use gcc to compile the file into an object, if I use g++ to compile the object I get no error.

I am not even sure what the error means, I have a feeling it has to do with the array stored in the Matrix returned by MultiplyMatrices.

If you need to see more code let me know.

This code is from this tutorial: OpenGL Book Chapter 4

p.s. I would like to keep this code strict iso/ansi, if there is no other solution however, then I'll just have to deal with it.

EDIT: I ended up going with creating a temporary Matrix then copying the array.

Matrix tempMatrix;

...

tempMatrix = MultiplyMatrices(m, &translation);
memcpy(m->m, tempMatrix.m, sizeof(m->m));

解决方案

The return value of MultiplyMatrices() is not an lvalue (like the return value of any function), which means that you can't take its address. Evaluating an array (including an array member of a structure) implicitly takes the address of the first element, so you can't do that.

You can, however, use simple assignment of the containing struct:

*m = MultiplyMatrices(m, &translation);

As long as your struct only contains the one element as you have shown, this is exactly the same.

这篇关于Ç - 无效使用非左值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆