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

8
  • dont forget the return from main Commented Apr 22, 2016 at 8:05
  • Possible duplicate of Why are arrays not assignable in C/C++? Commented Apr 22, 2016 at 8:07
  • @deimus main returns 0 if there is no return statement Commented Apr 22, 2016 at 8:23
  • @tobi303 thats true, but having explicit return in main is considered a good practice ;) Commented Apr 22, 2016 at 8: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 Commented Apr 22, 2016 at 8:49

3 Answers 3

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

Comments

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

Comments

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

Comments

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.