1

質問が他の場所で既に行われていることは知っていますが、ここで何が問題なのかわかりません。2 つの配列が構造内にあるため、何か間違っている可能性があります。

(編集:他のコードから構造を取得していますが、変更できません)

float の 2 つの配列を関数に渡してから、操作の結果を最初の配列に保存しようとしています。

core.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);
}

コア.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]’

完全を期すために、ここに私が呼び出している関数がありますが、コンパイル時に問題はないようです:

another_file.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;

}
4

2 に答える 2

2

問題は、配列にポインタを割り当てているためです。C ++では、配列にポインタを割り当てることはできません。

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];
}

編集

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];
}
于 2013-02-06T08:35:40.353 に答える
0

floatへのポインタを返し、それを配列に保存しようとしているように見えます。structure_t.m_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; 

これはまだかなりエラーが発生しやすいです。yet_another_functionで16以外の別のサイズを新しくすると、バグが発生します。原則として、std::arrayまたはstd::vectorを使用し、値で返すことをお勧めします。コンパイラがC++11をサポートしている場合、値で返される移動セマンティクスはパフォーマンスの低下にはなりません。

于 2013-02-06T08:34:29.220 に答える