我有一個類和我想創建若干實例的數組,特別是矩陣類的數組:問題創建對象C++
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;
}
我的主要代碼是:
int main()
{
Matrix myArray[2] = { Matrix(6,6)};
return 0;
}
當我使用g ++編譯,我得到以下錯誤:
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’
我身邊做一些谷歌搜索,但我還沒有找到答案。假設我的構造函數有問題。 ;
後
@SB:你不能用C++轉發構造函數。 – 2010-05-04 16:34:27
哇另一個腦袋流逝..可怕的建議刪除。 – 2010-05-04 19:19:01