1

I know the question was already asked elsewhere, but I keep not getting what's wrong here. Maybe I'm doing something wrong because the two arrays are inside a structure.

(EDIT: I am getting the structure from some other code, and I cannot change it)

I'm trying to pass two arrays of float to a function, and then saving back the result of manipulation in the first array.

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

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

When I'm calling the function, however, I'm getting this error:

error: incompatible types in assignment of ‘float*’ to ‘float [16]’

For completeness, here is the function I'm calling, though it does not seem to do any trouble at compile time:

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;

}

2 Answers 2

2

The problem is because you are assigning pointer to array. In C++ you can't assign pointer to array.

s1.m_vector = MyFunction(s1.m_vector, s2.m_vector);
  ^^ array   ^^ return pointer  

You could use copy the return valute from MyFunction to s1.m_vector. But why do you need to reassign value to s1.m_vector anyway? You could just make MyFunction function take a ref to structure_t and modify m_vector internally.

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

Edit

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];
}
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, I edited the question. I cannot use a ref it basically because I'm performing matrix manipulation and I need to update the old matrix values with the new ones.
use a ref to vDest is perfect for that solution! Note, first parameter is not const, so you can modify its matrix.
I wrote matrix manipulation, I meant matrix multiplication. See my updated question. How can I perform matrix manipulation without corrupting the initial matrix values?
Then you need to copy the return value of MyFunction and delete the memory allocated by MyFunction, if you want to do it that way.
I don't really like that solution, but I think I'll do like that, just like PorkyBrain suggested too, or find some other workaround.
|
0

Looks like your returning a pointer to float and trying to save it to your array. structure_t.m_vector is an array not a pointer.

you could fix it like this:

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

this is still quite error prone, if you new a different size other than 16 in yet_another_function you got a bug. As a general rule using std::array or std::vector and returning by value would be suggested. If you compiler supports c++ 11 move semantics returning by value will not be a performance penalty.

2 Comments

So, how should I write that? Anyway I seemed to understand that in C++ writing float[], float[16] and float * is basically the same.
when passed as arguments to a function they all turn into float *, but on the left side of operator = they are different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.