Reputation: 49
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.
Upvotes: 0
Views: 714
Reputation: 86524
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.
Upvotes: 6