2

Im creating my own matrix library for education purposes. I created a constructor that accepts a 4x4 matrix in the form of

Matrix(float initMatrix[4][4]){
 //init operation here
}

The above constructor works fine when creating the object by creating a 2D array and then using that variable to initialize this object, for example:

float my_matrix[4][4] = {{...},{...},{...},{...}}; //shortened for brevity
Matrix matrix(my_matrix);

However, it fails to build when doing the following:

Matrix matrix({{...},{...},{...},{...}});

The compiler tells me

> cannot convert initializer list argument to 'float (*)[4]'

So I added another constructor that looks like this:

Matrix matrix(float matrix(*)[4]){}

I end up getting the following compiler error.

error: C++ requires a type specifier for all declarations
    Vytrix(float input(*)[4]){

Can someone show me a good flexible way of designing the constructors so that I can initialize the class in a clean way?

Khalid Akash
  • 127
  • 7
  • Try to initiallize using float lists `Matrix matrix(new float[4][4]{{...},{...},{...},{...}});`. – Azhy Aug 01 '18 at 23:21
  • 1
    Related: https://stackoverflow.com/q/16455029/2602718 – scohe001 Aug 01 '18 at 23:22
  • https://stackoverflow.com/questions/7657910/arrays-initialization-body-as-function-parameter-c-array-is-it-possible and https://stackoverflow.com/questions/31537349/initialiser-list-passed-as-function-parameter-for-array and https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – Jerry Jeremiah Aug 01 '18 at 23:25

4 Answers4

2

You're using C++, so you can use std::array!

std::array is a little wonky with initialization, so you'll need an extra set of curly braces. Here's a working example:

#include <array>

class foo
{
public:
    foo(std::array<std::array<int, 2>, 3> arr) { }
};

int main()
{
    foo f({{{1, 2}, {3, 4}, {5, 6}}});
}
scohe001
  • 13,879
  • 2
  • 28
  • 47
2

The syntax would be:

Matrix(const float (&m)[4][4]);
Jarod42
  • 173,454
  • 13
  • 146
  • 250
  • This should really be a comment, not an answer. – L. Kue Aug 01 '18 at 23:36
  • 2
    @L.Kue: That would solve OP's issue, so it is an answer. You might consider it as too short answer. Comments are mostly to ask "clarification"s. – Jarod42 Aug 01 '18 at 23:42
  • @Jarod42 Thank you for your response, it works well! Does this work because I am essentially passing a pointer by declaring it like this: Matrix matrix({{......}})? – Khalid Akash Aug 01 '18 at 23:46
  • @KhalidAkash: C-Arrays cannot be passed by value, so you have to pass them by pointer/reference. `Matrix(float initMatrix[4][4])` is in fact `Matrix(float (*initMatrix)[4])`. – Jarod42 Aug 01 '18 at 23:50
1

If you want to force the dimensions you can accept a const reference to an array instead of passing the array directly:

struct Matrix {
    Matrix(const float (&initMatrix) [4][4]) {}
};

int main()
{
    Matrix m({{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}});
    return 0;
}

Try it here: https://onlinegdb.com/ByzOQTkrQ

This is a good answer: https://stackoverflow.com/a/17569578/2193968

Jerry Jeremiah
  • 7,408
  • 2
  • 21
  • 27
1

You could just do:

struct Matrix
{
    float initMatrix[4][4];
};

int main()
{
    Matrix m{ { { 1,2,3,4 },{ 5,6,7,8 },{ 9,10,11,12 },{ 13,14,15,16 } } };
    return 0;
}

https://ideone.com/WeZjbY

Killzone Kid
  • 5,839
  • 3
  • 12
  • 33