我写了两个类Vector和Matrix,其中我使用copy
将动态数组从一个对象复制到另一个对象,用于复制构造函数和运算符=
。如果我将这些类的每个代码分别放到main
,它可以正常工作,但如果我同时放入它们,则会发生错误。
这是我的代码:
matrix.h
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
template <class U>
class Matrix {
int row, col;
U **data;
public:
Matrix(int, int);
Matrix(const Matrix&);
Matrix operator = (const Matrix&);
~Matrix();
};
template <class U>
Matrix<U>::Matrix(int row, int col) {
this->row = row;
this->col = col;
data = new U *[row];
for (int i = 0; i < col; ++i)
data[i] = new U[col];
}
template <class U>
Matrix<U>::Matrix(const Matrix& mt) {
if (this != &mt) {
row = mt.row;
col = mt.col;
data = new U *[row];
for (int i = 0; i < col; ++i)
data[i] = new U[col];
for (int i = 0; i < row; i++)
copy(&mt.data[i][0], &mt.data[i][col+1], data[i]);
//memcpy(data[i], mt.data[i], col*sizeof(data[i]));
}
}
template <class U>
Matrix<U>::~Matrix() {
for (int i = 0; i < row; i++) {
delete [] data[i];
}
delete [] data;
}
template <class U>
Matrix<U> Matrix<U>::operator = (const Matrix& mt) {
if (row == mt.row and col == mt.col) {
for (int i = 0; i < row; i++)
copy(&mt.data[i][0], &mt.data[i][col], data[i]);
//memcpy(data[i], mt.data[i], col*sizeof(data[i]));
return *this;
}
else throw runtime_error("Matrices must have same size.");
return *this;
}
vector.h
#include <iostream>
#include <stdlib.h>
#include <stdexcept>
using namespace std;
template <class type>
class Vector {
int size;
type *data;
public:
Vector(int);
Vector(const Vector&);
~Vector();
Vector operator = (const Vector&);
};
template <class type>
int Vector<type>::get_size() {
return size;
}
template <class type>
Vector<type>::Vector(int size) {
if (size < 2) throw runtime_error("Size must be greater than 1.");
else {
this->size = size;
data = new type[size];
}
}
template <class type>
Vector<type>::Vector(const Vector& that) {
size = that.size;
data = new type[size];
copy(&that.data[0], &that.data[size+1], data);
//memcpy(data, that.data, size*sizeof(data));
}
template <class type>
Vector<type> Vector<type>::operator = (const Vector& that) {
cout<<"Assigning..."<<endl;
if (size == that.size) {
copy(&that.data[0], &that.data[size], data);
//memcpy(data, that.data, size*sizeof(data));
return *this;
}
else throw runtime_error("Vectors must have same size.");
return *this;
}
的main.cpp
#include <iostream>
#include "vector.h"
#include "matrix.h"
#include <stdexcept>
using namespace std;
int main() {
try {
Vector<int> a(3), c(3);
Matrix<int> b(2,2), d(2,2);
// Vector
cin>>a;
cout<<a;
c=a;
cout<<c;
//Matrix
cin>>b;
d = b;
cout<<d;
}
catch (exception &e) {
cout<<e.what()<<endl;
}
}
和错误:
vector_matrix_poly_demo: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Aborted (core dumped)
其余的代码(重载运算符>>
和<<
)我没有放在这里因为它工作正常。这个错误只是在linux中,我使用的是Ubuntu 14.04,如果我先将copy替换为memcpy或Matrix代码块,则不会发生错误。
最好的问候,抱歉我的英语不好。