gpt4 book ai didi

c++ - 具有类型名称的矩阵总和

转载 作者:行者123 更新时间:2023-11-28 01:14:20 25 4
gpt4 key购买 nike

所以我想用运算符重载对 2 矩阵求和,但我在 m2 矩阵变量上得到一个错误,说“表达式必须具有整数或无作用域的枚举类型”

template <class type>
class Matrix
{
public:
Matrix(type row, type column, type index);
Matrix<type> operator+(Matrix<type>* other);
type getrow();//not important
type getcolumn();
type** getMatrix();
private:
type row;
type column;
type index;
type** matrix;
};

template<class type>
Matrix<type> Matrix<type>::operator+(Matrix<type>* other)
{
if(this->row==other->getrow()&&this->column==other->getcolumn())
{
for (int i = 0; i < this->row; i++)
{
for (int k = 0; k < this->column; k++)
{
other->getMatrix()[i][k] += this->matrix[i][k];
}
}
}
return other;
}

int main()
{
Matrix<int>* m1 = new Matrix<int>(1, 3, 1);//(row,column,index)
Matrix<int>* m2 = new Matrix<int>(3, 3, 3);

m1 = m1 + m2;//error on m2
}

我该如何解决这个问题,谢谢你的帮助:)

最佳答案

即使没有完整的错误输出,这里的问题也很容易弄清楚(但将来请包括完整的错误输出)。

m1 + m2你添加了两个指针,而不是两个Matrix<int>对象( m1 + m2 本质上等同于 &m1[m2] )。

要么不使用指针,要么new :

Matrix<int> m1(1, 3, 1);//(row,column,index)
Matrix<int> m2(3, 3, 3);

或者解引用指针:

*m1 = *m1 + *m2;

关于c++ - 具有类型名称的矩阵总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59190953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com