jmasterx
jmasterx

Reputation: 54103

Error when setting 2D array to 2D array

I have this constructor:

Transform::Transform( float matrix[4][4] )
{
    m = matrix;
}

And this class definition:

class Transform
{
    float m[4][4];
public:
    Transform();
    Transform(float matrix[4][4]);

But this does not compile.

What could be wrong?

Error 1 error C2440: '=' : cannot convert from 'float [][4]' to 'float [4][4]' c:\Users\Josh\Documents\agui\trunk\src\Agui\Transform.cpp 75

Thanks

Upvotes: 0

Views: 74

Answers (3)

Code-Apprentice
Code-Apprentice

Reputation: 83527

Even though you declare the parameter to your constructor as float matrix[4][4], the compiler ignores the first 4.

Upvotes: 1

Karthik T
Karthik T

Reputation: 31952

If you are using c++11 try to change float matrix[4][4] to std::array<std::array<float,4>,4>

It is a mouthful, but it supports such operations that c arrays do not natively support.

You could do something like this to clean up the syntax.

typedef std::array<std::array<float,4>,4> Matrix;

Now you can do

Matrix myMatrix;

p.s If you are not using C++11, you could use vector instead of array. It is a little different from array, but adds more features as well, and after you set it up access is identical.

Upvotes: 4

David Venegoni
David Venegoni

Reputation: 508

Karthik's answer is excellent, alternatively, you could also do...

  for(int i = 0; i < 4; i++)
  {
     for(int j = 0; j < 4; j++)
      {
         m[i][j] = matrix[i][j];
      }
  }

The principle is the same that WhozCraig mentioned in the comment.

Upvotes: 3

Related Questions