0

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.

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@SB: You can't forward constructors in C++.
0

need ; after float **p

1 Comment

Accidentally deleted it when editing my post! I guess copy and paste is not my forte.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.