尝试将矩阵与常数重载运算符相乘两次
Trying to multiply a matrice with a constant, overload operator twice?
我目前正在尝试将程序中的一个矩阵与常数相乘,但由于没有现有的方法可以做到这一点,我想我必须重载一个运算符或类似的运算符。但由于我已经超载了一次"*"运算符。它还需要从左边而不是从右边取值,或者更具体地说是常数。如何继续?
感谢所有的帮助!
过载*操作员
matrix operator * (matrix const arg){
double sum = 0;
int x = 0;
int rowY = 0;
int rowX = 0;
this->matrix_array_multiply = new double[row*arg.col];
for (int position = 0; position < row*arg.col; position++, x++){
if (arg.matrix_array[x*arg.row] == (arg.matrix_array[arg.row*arg.col])){
//If last number in second matrix, reset these values and proceed with next row of Y-values.
x = 0;
rowY++;
}
for (int y = 0; y < arg.row; y++, rowX++){
sum = sum + (matrix_array[y + (rowY*col)]) * (arg.matrix_array[x + (rowX*arg.col)]);
}
matrix_array_multiply[position] = sum;
rowX = 0;
sum = 0;
}
matrix new_matrix_multiply(matrix_array_multiply, row, arg.col); //Create new instance of new matrix.
return new_matrix_multiply; //Return new matrix.
}
不同矩阵和运算符的用法:
int main () {
double a[] = { 3, 0, 3, 4,
3, 4, 2, 4,
5, 3, 2, 1 };
double b[] = { 6, 3, 5, 7,
9, 8, 6, 4,
6, 5, 3, 1 };
double c[] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 2, 1, 1,};
double d[] = { 6, 5, 4, 3,
2, 1, 0, 1,
2, 3, 4, 5,
6, 7, 8, 9};
double e[] = { 1, 2, 1,
3, 5, 7,
9, 7, 3};
matrix matrix1(a, 3, 4); //Instance of the class matrix: array, rows, columns.
matrix matrix2(b, 3, 4);
matrix matrix3(c, 3, 4);
matrix matrix4(d, 4, 4);
matrix matrix5(e, 3, 3);
matrix matrix6 = (matrix1 + matrix2);
matrix matrix7 = (matrix2 - matrix1);
matrix matrix8 = (matrix3 * matrix4);
matrix matrix9 = ~matrix5;
matrix matrix10 = (5.7 * matrix5); // Error: no operator "*" matches these operands, operand types are: double * matrix
}
注意:我刚开始学习c++,这是家庭作业的一部分。
matrix matrix10 = (5.7 * matrix5);
为了实现这一点,您必须定义一个带有以下签名的免费函数:
matrix operator*(double c, matrix const & m) //non-member function
{
//your code
}
同样,你也想定义这个:
matrix operator*(matrix const & m, double c)
{
return c * m; //call the other overload!
}
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- 为什么转换运算符调用复制构造函数两次,而等效函数只调用它一次
- 为什么与<运算符的向量比较会比较每个项目两次?
- 在条件运算符中使用对象两次会产生 UB 吗?
- 重载运算符 new(),为什么构造函数被调用两次?
- 在 c++ 中,如何修改运算符以便在同一语句中连续调用它两次
- 范围分辨率运算符被两次使用
- 为什么在C 中超载邮政增量运算符两次调用构造函数
- C++重载运算符两次,一个返回非常量引用,另一个返回常量引用,首选项是什么
- 为什么编译器调用 -> 运算符两次
- 尝试将矩阵与常数重载运算符相乘两次
- 重载 * 乘法运算符两次作为成员函数
- 用不同的参数重载运算符两次
- 毁灭战士 3 来源:为什么表示 2 向量的类会重载下标运算符两次
- 类成员运算符new,调用构造函数两次
- Destructor被调用两次,而没有复制构造函数或赋值运算符被调用