1

I have a class called MATRIX with element[4][4] array and some other functions for adding, subtracting, etc.

I want to insert an entire array value into the matrix from the main function, but I keep getting errors.

Here's the code:

class MATRIX
{
public:
    float ele[4][4];
    float rows;
    float cols;

    MATRIX Add(MATRIX m);
    MATRIX Subtract(MATRIX m);
    MATRIX Multiply(MATRIX m);
    MATRIX Transpose();
    MATRIX Initialize();
};

int main(int argc, char ** argv){

float x_angle = 0, y_angle = 0, z_angle = 0;
MATRIX xRotation, yRotation, zRotation;

xRotation.ele = {
    { 1, 0, 0, 0 },
    { 0, cosf(x_angle), -sinf(x_angle), 0 },
    { 0, sinf(x_angle), cosf(x_angle), 0 },
    { 0, 0, 0, 1 } };
}

The error message tells me that the expression must be a modifiable lvalue

Jason J.Y. Kim
  • 183
  • 2
  • 12
  • dont forget the return from main – deimus Apr 22 '16 at 08:05
  • Possible duplicate of [Why are arrays not assignable in C/C++?](http://stackoverflow.com/questions/25230714/why-are-arrays-not-assignable-in-c-c) – Alexander Apr 22 '16 at 08:07
  • @deimus `main` returns 0 if there is no `return` statement – 463035818_is_not_an_ai Apr 22 '16 at 08:23
  • @tobi303 thats true, but having explicit return in main is considered a good practice ;) – deimus Apr 22 '16 at 08:34
  • @deimus can you give a reference or some reasoning for this? for me highest priority good practice is not to write more code than necessary – 463035818_is_not_an_ai Apr 22 '16 at 08:49
  • guys, cool down. I didn't put return 0 on the above example because I was lazy and the entire code is just huge. I have it in my actual code. – Jason J.Y. Kim Apr 22 '16 at 08:50
  • @tobi303 reasonnings are many but they are not about must but for keeping what is called good practice, so they are - 1. For compatibility with C90 otherwise the bahaviour is undefined. 2. Main is declated to return so it "should" 3. Correct return status management, imagine an `exit()` is called in main, it will skip deallocation of local object instances. 4. etc – deimus Apr 22 '16 at 09:39
  • @deimus ok thanks, 1 is actually already enough to convince me as I prefer to adjust my habits such that I can write same code in different languages as far as this is possible – 463035818_is_not_an_ai Apr 22 '16 at 11:13

3 Answers3

2

The way you are trying to add values is mean to be used on initializatoin, which is not your case. You can use std::copy

float array[4][4] = {
    { 1, 0, 0, 0 },
    { 0, cosf(x_angle), -sinf(x_angle), 0 },
    { 0, sinf(x_angle), cosf(x_angle), 0 },
    { 0, 0, 0, 1 }
};


std::copy(&array[0][0], &array[0][0]+(4*4), &xRotation.ele[0][0]);
Nadir
  • 1,799
  • 12
  • 20
0

2d arrays are often a pain, avoid them if you can (declaring a 1d array of sizey*sizex and accessing array[y*SIZEX+x])

class MATRIX {
public:
    float ele[4*4];

    void set(float* m)  { for (int i=0; i<16; i++) ele[i] = m[i]; }
private:
    inline float& get(int y, int x) { return ele[4*y+x]; } // to give you an idea
};

in main:

MATRIX x;
x.set({ ... });
Exceptyon
  • 1,584
  • 16
  • 23
0

c arrays are not assignable, but std::array<> is assignable from either an array or a compatible std::initializer_list<>. Therefore, this will work:

#include <cmath>
#include <array>

using floats = std::array<float, 4>;
using floats2 = std::array<floats, 4>;

class MATRIX
{
public:
    floats2 ele;
    float rows;
    float cols;

    MATRIX Add(MATRIX m);
    MATRIX Subtract(MATRIX m);
    MATRIX Multiply(MATRIX m);
    MATRIX Transpose();
    MATRIX Initialize();
};

int main(int argc, char ** argv){

    float x_angle = 0, y_angle = 0, z_angle = 0;
    MATRIX xRotation, yRotation, zRotation;

    xRotation.ele = {
        floats{ 1, 0, 0, 0 },
        floats{ 0, cosf(x_angle), -sinf(x_angle), 0 },
        floats{ 0, sinf(x_angle), cosf(x_angle), 0 },
        floats{ 0, 0, 0, 1 } };
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142