【发布时间】:2020-03-01 19:59:28
【问题描述】:
所以我试图为我的“矩阵”类(+ 和 +=)重载两个运算符。我正在尝试使+ 可链接和+= 不可链接:
template <class T>
Matrix<T>& Matrix<T>::operator+=(const Matrix& M)
{
if (this->m_capacity != M.capacity())
{
throw std::out_of_range("Input is invalid");
}
for (unsigned int i = 0; i < M.rows(); i++)
{
for (unsigned int j = 0; j < M.cols(); j++)
{
this->m_vec[i + m_cols * j] += M(i, j);
}
}
return *this;
}
template <class T>
Matrix<T> operator+(Matrix<T> M1, Matrix<T>& M2)
{
if (M1.capacity() != M2.capacity())
{
throw std::out_of_range("Input is invalid");
}
return M1 += M2;
}
它编译得很好,没有问题,但是当我尝试对此进行单元测试时,整个测试程序在尝试链接 + 运算符时崩溃。
例子:
TEST(add, Matrix)
{
Matrix<int> M1 = Matrix<int>(2, 3);
Matrix<int> M2 = { 1, 2, 3, 4 };
Matrix<int> M3 = M2;
Matrix<int> M4 = { 2, 4, 6, 8 };
ASSERT_THROW(M1 + M2, std::out_of_range);
ASSERT_EQ((M2 + M3) == M4, true);
M2 += M3;
M2 = M4 + M4 + M4; // As soon as this line is added, it crashes, without it, test works fine
ASSERT_EQ(M2 == M4, true);
}
任何想法为什么它崩溃?如何重写我的运算符重载,以便 ´+´ 可链接(而 += 不是)?
编辑:
这是我的= 运营商(根据要求)
template <class T>
void Matrix<T>::operator=(Matrix & M){
T*temp = new T[M.m_capacity];
for(unsigned int j = 0; j < M.m_capacity; j++){
temp[j] = M.m_vec[j];
}
delete[] this -> m_vec;
size_t rows = M.get_m_rows();
size_t cols = M.get_m_cols();
this -> m_rows = rows;
this -> m_cols = cols;
this -> m_vec = new T [rows*cols];
this -> m_capacity = rows*cols;
for(size_t i = 0;i < rows;i++){
for(size_t j = 0;j < cols;j++){
this -> m_vec[i*cols+j] = temp[i*cols +j];
}
}
delete [] temp;
}
编辑2: 添加了更多上下文(根据请求、标头、构造函数等)
标题:
template <class T>
class Matrix {
public:
// constructor
Matrix(unsigned int n);
Matrix(unsigned int n, unsigned int m);
Matrix();
Matrix(const T n);
Matrix(Matrix &obj);
~Matrix();
Matrix(Matrix &&obj);
Matrix(std::initializer_list<T> l);
// operators
void operator=(Matrix & obj);
T& operator()(unsigned int row, unsigned int col);
Matrix& operator=( Matrix &&obj);
Matrix& operator+=(const Matrix& M)
void operator+=(const T number);
void operator-=(const T number);
void operator-=(Matrix &obj);
void operator*=(const T number);
void operator*=(Matrix &obj);
bool operator==(Matrix & rhs);
private:
std::size_t m_rows;
std::size_t m_cols;
std::size_t m_capacity;
T * m_vec;
};
复制构造函数:
template <class T>
Matrix<T>::Matrix(Matrix &obj){
size_t rows = obj.get_m_rows();
size_t cols = obj.get_m_cols();
this -> m_rows = rows;
this -> m_cols = cols;
this -> m_vec = new T [rows*cols];
this -> m_capacity = rows*cols;
for(size_t i = 0;i < rows;i++){
for(size_t j = 0;j < cols;j++){
this -> m_vec[i*cols+j] = obj(i,j);
}
}
}
析构函数:
template <class T>
Matrix<T>::~Matrix(){
delete [] m_vec;
}
移动构造函数(可能损坏)
template <class T>
Matrix<T>::Matrix(Matrix &&obj){
size_t rows = obj.get_m_rows();
size_t cols = obj.get_m_cols();
this -> m_rows = rows;
this -> m_cols = cols;
this -> m_vec = new T [rows*cols];
this -> m_capacity = rows*cols;
m_vec = nullptr;
}
移动分配(可能已损坏)
template <class T>
Matrix<T>& Matrix<T>::operator=(Matrix &&obj){
if (this !=&obj)
{
delete [] m_vec;
obj.m_rows = 0;
obj.m_cols = 0;
obj.m_capacity = 0;
obj.m_vec = nullptr;
}
return *this;
}
【问题讨论】:
-
您的
operator =正确吗?请将其包含在问题中。 -
请提供完整的minimal reproducible example,至少是
Matrix的特殊成员函数的定义。您的复制/移动构造函数或复制/移动分配似乎已损坏。+=在您的示例中也可以链接(这是一件好事)。 -
@Schytheron 复制赋值运算符应始终将其参数作为
const引用。 (顺便说一句。您可能还希望operator+采用const引用的第二个参数。)复制构造函数和析构函数看起来如何?请包括至少Matrix的定义和所有特殊成员函数的定义。将您的问题减少到minimal reproducible example。 -
您的赋值运算符可以只用 4 行调用
std::swap的代码来编写。使用copy / swap idiom。Matrix t(obj); std::swap(t.m_rows, m_rows); std::swap(t.m_cols, m_cols); std::swap(t.m_capacity, m_capacity); std::swap(t.m_vec, m_vec); return *this; -
你可以通过使用
vector而不是裸新闻来消除一整类错误
标签: c++ matrix operator-overloading chaining method-chaining