views:

68

answers:

2

I have a class and I want to create an array of a number instances, specifically a matrix class:

class Matrix {    
public:
    Matrix(int sizeX, int sizeY);
    Matrix();
    ~Matrix();
    ....//omiting the rest here
private:
    int dx, dy;
    float **p
    void allocArrays() {
        assert(dx>0);
        assert(dy>0);
        p = new float*[dx];
        for (int i = 0; i < dx; i++){
            p[i] = new float[dy]; 
        }
    }
};
Matrix::Matrix(int sizeX=1, int sizeY=1)
: dx(sizeX),dy(sizeY)  {
    allocArrays();
    for (int i = 0; i < dx; i++)    {
        for (int j = 0; j < dy; j++) {
            p[i][j] = 0;
        }
    }
}


Matrix::Matrix(const Matrix& m) : dx(m.dx), dy(m.dy) {
    allocArrays();
    for (int i=0; i<dx; ++i) {
        for (int j=0; j<dy; ++j) {
            p[i][j] = m.p[i][j];
        }
    }
}

Matrix::~Matrix() {
    for (int i = 0; i < dx; i++) {
        delete [] p[i]; 
    }
    delete [] p;
    p = 0;
}

My main code is:

int main()
{
    Matrix myArray[2] = { Matrix(6,6)};
    return 0;
}

When I compile with g++ I get the following error:

matrixTesting.cc: In function ‘int main()’:
matrixTesting.cc:197: error: call of overloaded ‘Matrix()’ is ambiguous
matrixTesting.cc:11: note: candidates are: Matrix::Matrix()
matrixTesting.cc:44: note:                 Matrix::Matrix(int, int)
matrixTesting.cc:197: warning: unused variable ‘myArray’

I did some searching around google but I haven't found an answer. Suppose there is a problem with my constructor.

A: 

need ; after float **p

Taryk
Accidentally deleted it when editing my post! I guess copy and paste is not my forte.
+6  A: 

Your Matrix(int, int) constructor has all default-able arguments, which would make it as callable as the default constructor. You should either get rid of the default constructor, or make it so that at least one of the arguments to Matrix(int, int) is required.

cHao
@SB: You can't forward constructors in C++.
Georg Fritzsche
wow another brain lapse.. horrible advice deleted.
SB