我已将范围缩小到派生类复制构造函数,但我不确定原因。 编辑: M,N和数据是私人的。我收到的错误是'无效的分配大小:4294967295字节' - 我明白是将-1传递给新的时产生的。我不确定为什么会发生这种情况,除非数据在班级交流时丢失。无效的分配大小(在派生类复制构造函数中)
BinaryMatrix::BinaryMatrix(const BinaryMatrix& copy) : Matrix(copy)
{
//cout << "Copy Constructor\n";
M = copy.M;
N = copy.N;
data = new double[M*N]; //This line causes the allocation error
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
data[i*N+j] = copy.data[i*N+j];
}
}
}
以上是我的派生复制构造函数,它会导致错误。我已经标记了分配线。
我只能假设M和N没有被正确读取。虽然我不确定为什么。我将包括派生和基础构造函数,以及基本副本。
感谢您的任何帮助。
MATRIX(BASE)构造
Matrix::Matrix(int M, int N, double* input_data)
{
this->M = M;
this->N = N;
//cout << "Matrix Constructor\n";
data = new double[M*N];
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
data[i*N+j] = input_data[i*N+j];
}
}
delete [] input_data;
}
MATRIX(BASE)拷贝构造
Matrix::Matrix(const Matrix& copy)
{
//cout << "Copy Constructor\n";
M = copy.M;
N = copy.N;
data = new double[M*N];
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
data[i*N+j] = copy.data[i*N+j];
}
}
}
BINARYMATRIX(派生)构造
BinaryMatrix::BinaryMatrix(int M, int N, double* input_data) : Matrix(M, N, input_data)
{
data = new double[M*N];
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
this->data[i*N+j] = this->getRead(i, j);
}
}
double thr_val = this->Mean();
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (this->data[i*N+j] > thr_val)
this->data[i*N+j] = 1;
if (this->data[i*N+j] < thr_val)
this->data[i*N+j] = 0;
}
}
}
什么是错误? – Castilho 2012-03-17 15:19:49
Doh - 对不起,我现在将其编辑为主要问题。 – LBHoward 2012-03-17 15:21:12
这是运行时错误还是编译时错误? – je4d 2012-03-17 15:21:43