Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a constructor that accepts 2D array

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?

like image 727
Khalid Akash Avatar asked Jul 01 '26 09:07

Khalid Akash


1 Answers

The syntax would be:

Matrix(const float (&m)[4][4]);
like image 81
Jarod42 Avatar answered Jul 03 '26 05:07

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!