0

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

2
  • 2
    I'd recommend std::array if you can use it. It does assignment. Commented Jan 9, 2013 at 2:06
  • 3
    Arrays are not copyable in this manner. You need to copy the contents (either through indexed access or a memory-move algorithm like std::copy(), memcpy() or memmove(). Of course, belay all that and do what chris says above. Use your stdlib. Its whats for dinner. Commented Jan 9, 2013 at 2:07

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

With a typedef so it isn't a mouthful.
@Pubby, It's C++11 anyway. template<typename T> using array4 = std::array<T, 4>; :) Depends on what changes when you use it, really.
@Pubby very true, I have added it
3

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.

Comments

1

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

4 Comments

I guess then it sees it as float*[4]
@Milo You will need to ask a language Nazi for the difference between float [][4] (as in your error message) and float *[4]. I typically treat them as the same thing (which probably isn't technically accurate).
Not a lang-nazi, but so you know , float(*)[4]. With a variable decl it would be float (*ar)[4]. And its the first dimension (the superior dim) that is ignored; not all but the last. The sub-dimensions are used to calculate linear offset into the backdrop of the memory behind the array.
@WhozCraig Thanks for the clarification. I removed that sentence from my answer.

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.