Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;将数组传递给函数错误:赋值中的类型不兼容_C++_Arrays_Function - Fatal编程技术网

C++ C++;将数组传递给函数错误:赋值中的类型不兼容

C++ C++;将数组传递给函数错误:赋值中的类型不兼容,c++,arrays,function,C++,Arrays,Function,我知道其他地方已经有人问过这个问题,但我一直不知道这里出了什么问题。也许我做错了什么,因为这两个数组在一个结构中 (编辑:我从其他代码获取结构,无法更改) 我试图将两个浮点数组传递给一个函数,然后将操作结果保存在第一个数组中 核心.h: typedef struct{ //other stuff float m_vector[16]; } structure_t; class CoreClass{ private: structure_t s1;

我知道其他地方已经有人问过这个问题,但我一直不知道这里出了什么问题。也许我做错了什么,因为这两个数组在一个结构中

(编辑:我从其他代码获取结构,无法更改)

我试图将两个浮点数组传递给一个函数,然后将操作结果保存在第一个数组中

核心.h:

typedef struct{
    //other stuff
    float m_vector[16];
} structure_t;

class CoreClass{
    private:
        structure_t s1;
        structure_t s2;

       float *MyFunction(const float *vDest, const float *vNew);
}
core.cpp:

#include "core.h"
#include "another_file.h"

void anotherFunction(){
    //....
    s1.m_vector = MyFunction(s1.m_vector, s2.m_vector); //error here
    //....    
}

float *CoreClass::MyFunction(const float *vDest, const float *vNew){
    return yet_another_function(vDest, vNew);
}
但是,当我调用函数时,我得到以下错误:

error: incompatible types in assignment of ‘float*’ to ‘float [16]’
为了完整起见,下面是我正在调用的函数,尽管它在编译时似乎没有任何问题:

另一个_文件.h

static __inline float *yet_another_function(const float *vDest, const float *vNew){
    float *tmp = new float[16];
    //tmp = matrix multiplication (vDest * vNew)
    for(int i=0; i<4; i++)
        for(int j = 0; j<4;j++)
            for(int k = 0; 4; k++)
                tmp[i + j*4] += vDest[i + k*4] * vNew[k + j*4];

    return tmp;

}
static\u内联浮点*还有另一个函数(const float*vDest,const float*vNew){
浮动*tmp=新浮动[16];
//tmp=矩阵乘法(vDest*vNew)

for(int i=0;i看起来像是返回一个指向float的指针并试图将其保存到数组中。structure\t.m\u vector是数组而不是指针

您可以这样修复它:

float * temp = MyFunction(s1.m_vector, s2.m_vector);
for(int i=0; i<16;i++)
    s1.m_vector[i] = temp[i];
delete[] temp; 
float*temp=MyFunction(s1.m_向量,s2.m_向量);

对于(int i=0;i问题,因为您将指针分配给数组。在C++中,不能为数组分配指针。< /P>
s1.m_vector = MyFunction(s1.m_vector, s2.m_vector);
  ^^ array   ^^ return pointer  
您可以使用将返回值从
MyFunction
复制到
s1.m_vector
。 但是,为什么您需要重新为
s1.m_vector
赋值呢?您可以让
MyFunction
函数引用
structure_t
并在内部修改
m_vector

void MyFunction(structure_t& vDest, const structure_t& vNew)
{
    vDest.m_vector[0] = vNew.m_vector[0];
    //...
    vDest.m_vector[15] = vNew.m_vector[15];
}
编辑


那么,我应该如何写呢?不管怎样,我似乎理解在C++中写浮点[],浮点[16 ]。和float*基本相同。当作为参数传递给函数时,它们都会变成float*,但在运算符=的左侧,它们是不同的。对不起,我编辑了这个问题。我不能使用ref it,因为我正在执行矩阵操作,我需要用新的值更新旧的矩阵值。使用ref to vDest是最好的请注意,第一个参数不是常量,因此您可以修改它的矩阵。我编写了矩阵操作,我的意思是矩阵乘法。请参阅我的更新问题。如何在不损坏初始矩阵值的情况下执行矩阵操作?然后您需要复制MyFun函数的返回值并删除MyFun分配的内存如果你想这样做的话,我不太喜欢这个解决方案,但我想我也会这样做,就像PorkyBrain建议的那样,或者找到其他解决办法。
yet_another_function(structure_t* t, structure_t& vDest, const structure_t& vNew)
{
    // blah blah
    t->m_vector[i + j*4] += vDest.m_vector[i + k*4] * vNew.m_vector[k + j*4];
}