0

I've got a matrix class with a 2d array of elements as a member variable. And there's a custom ctor with a 2d array as the param. The weird thing was when I tried testing it, it throws error C2440: error C2440: '' : cannot convert from 'float [3][3]' to 'Matrix'. Here's the code:

Matrix.h

class Matrix
{
    public:
        Matrix();
        ~Matrix();
        Matrix(const Matrix& other);
        Matrix( float e_11, float e_12, float e_13,
                  float e_21, float e_22, float e_23, \
                  float e_31, float e_32, float e_33);

        Matrix(float *elements[]);

    private:

        float* m_ElementsA; //< The array of elements of the vector 
}

Matrix.cpp

#include "../include/Matrix.h"

Matrix::Matrix()
{
    m_ElementsA = new float[9];
    for (int i = 0; i < 9; ++i)
    {
        *(m_ElementsA + i) = 0.0f;
    }
}

Matrix::~Matrix()
{
    delete[] m_ElementsA;
}

Matrix::Matrix(const Matrix& other)
{
    m_ElementsA = new float[9];
    unsigned int row, column;

    for (int i = 0; i < 9; ++i)
    {
        GetRowColumnFromIndex(i, row, column);
        *(m_ElementsA + i) = other.GetElement(row, column);
    }
}

Matrix::Matrix( float e_11, float e_12, float e_13, \
                float e_21, float e_22, float e_23, \
                float e_31, float e_32, float e_33)
{
    m_ElementsA = new float[9];

    *m_ElementsA = e_11;
    *(m_ElementsA + 1) = e_12;
    *(m_ElementsA + 2) = e_13;
    *(m_ElementsA + 3) = e_21;
    *(m_ElementsA + 4) = e_22;
    *(m_ElementsA + 5) = e_23;
    *(m_ElementsA + 6) = e_31;
    *(m_ElementsA + 7) = e_32;
    *(m_ElementsA + 8) = e_33;
}

Matrix::Matrix( float *elements[])
{
    m_ElementsA = new float[9];
    unsigned int row, column;

    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            *(m_ElementsA + (i * 3) + j) = elements[i][j];
        }
    }
}

main.cpp

int _tmain(int argc, _TCHAR* argv[])
{   
    float arr[3][3] =
    {
        {1.1f, 2.2f, 3.3f},
        {4.4f, 5.5f, 6.6f},
        {7.7f, 8.8f, 9.9f}
    };

    Matrix mat3 = Matrix(arr);

    return(0);
}

What did I do wrong here? Thanks in advance.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436

1 Answers1

0

You have an invalid conversion from a float[3][3] array to float ** (your constructor):

Matrix::Matrix(float *elements[])

since you're dealing with fixed 3x3 matrices, you should rather do something like:

Matrix::Matrix(float(&elements)[3][3]) // Reference to 3x3 array
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • Yeah I already did. It was because of the a few minutes window limitation to accept the answer that I didn't do it earlier. BTW, another question if you may. How do you do it if you want to pass an 2D vector with arbitrary length then? – Bawenang Rukmoko Pardian Putra Oct 29 '14 at 14:56
  • Since this is marked C++ I would recommend to use a std::vector. Anyway if you really wanted to do it with a C array you should specify the dimensions manually (http://stackoverflow.com/q/3991057/1938163) – Marco A. Oct 29 '14 at 14:58
  • 1
    Well, 2D std::vector would be too confusing to read except if you use typedef. But yeah, you're right. Thanks again. – Bawenang Rukmoko Pardian Putra Oct 29 '14 at 15:18