2

I am writing a program that handles matrices. The program enables you to enter the matrix at calling of the constructor:

CMatrix A(3,3,{{1,2,3},{4,5,6},{7,8,9}});

But it seams that there is a problem with sending a 2d array as int** to the constructor. How can I fix it? I would prefer to avoid template if possible. I am also looking for a clean way.

The following code faces with error:

error: no matching function for call to ‘CMatrix::CMatrix(int, int, <brace-enclosed initializer list>)’ CMatrix A(3,3,{{1,2,3},{4,5,6},{7,8,9}});

,

 note:   no known conversion for argument 3 from ‘<brace-enclosed initializer list>’ to ‘int**’

code:

#include <iostream>
using namespace std;

class CMatrix
{
    int rows,columns;
    int **members;
public:
    CMatrix(int rows,int columns);
    CMatrix(int rows,int columns,int **clone);
};

CMatrix::CMatrix(int rows,int columns)
{
    this->rows=rows;
    this->columns=columns;
    members=new int*[rows];
    for(int i=0;i<columns;i++)
        members[i]=new int[columns];
}

CMatrix::CMatrix(int rows,int columns,int **clone)
{
    CMatrix(rows,columns);
    for(int i=0;i<rows;i++)
        for(int j=0;j<columns;j++)
            members[i][j]=clone[i][j];
}

int main()
{
    CMatrix A(3,3,{{1,2,3},{4,5,6},{7,8,9}});
    ...
    return 0;
}

calling:

g++ -std=c++11 test.cpp
barej
  • 1,269
  • 2
  • 17
  • 50
  • 1
    You may just have to initialize the 2D array as an int** outside of the constructor call. Probably something about stacks and heaps, and where that array is being allocated. – DaaaahWhoosh Dec 09 '14 at 16:23
  • There is no array literal syntax. Consider using `std::array` so that brace-initialization will work. – 0x499602D2 Dec 09 '14 at 16:27
  • `int**` [is not a 2D array](http://stackoverflow.com/questions/2895433/casting-char-to-char-causes-segfault). You must have a template one way or another. – n. 'pronouns' m. Dec 09 '14 at 16:27
  • related http://stackoverflow.com/questions/8767166/passing-2d-array-to-function – hlscalon Dec 09 '14 at 16:47
  • @0x499602D2 what to write instead of 3 number in `std::array, 3>` since the matrices can be dynamic in my program? – barej Dec 10 '14 at 01:49
  • @barej [`std::vector>`](http://en.cppreference.com/w/cpp/container/vector) – 0x499602D2 Dec 10 '14 at 01:55

1 Answers1

2

Array initializers of the form { ... } can only be used to initialize an array. eg

int myArray[] = {1,2,3,4};

To pass in your array to your constructor, you must first build it. Either by building each row manually:

int row1[] = {1,2,3};
int row2[] = {4,5,6};
int row3[] = {7,8,9};
int * matrix[] = {row1, row2, row3};
CMatrix(3,3,matrix);

Or by dynamically building the whole thing:

int rows = 3;
int cols = 3;
int ** matrix = new int*[rows]; // allocate array of int pointers
for( int i = 0; i < rows; i++)
{
    matrix[i] = new int*[cols]; // allocate each array of ints
}
for (int i = 0; i<(rows*cols); i++)
{
    matrix[i/cols][i%cols] = i; //or whatever you need to set it to
}

CMatrix(rows,cols,matrix); // make your call

for( int i = 0; i < rows; i++)
{
    delete[] matrix[i]; //delete each int array
}
delete[] matrix; //delete matrix

You can immediately delete the matrix because CMatrix copies the values, not the array.

dconman
  • 90
  • 3