重载运算符时,二进制表达式的操作数无效
invalid operands to binary expression when overloading a operator
我正在尝试编写一个c++程序,该程序使用mul和添加运算符重载来实现一个简单的矩阵。我的矩阵存储在一个简单的Vector中。我还重载了()运算符,以便轻松地将值存储在矩阵中。
template < typename T1, int rows, int cols>
class Matrix{
public:
int row = rows;
int line = cols;
vector<T1> local_matrix;
Matrix(){
local_matrix = vector<T1>(rows*cols, 0);
}
T1 &operator () (int i, int j){
return local_matrix.at(i * cols +j);
}
const Matrix<T1, rows, cols> operator* (const Matrix<T1, rows, cols> &other) const{
Matrix<T1, other.row, other.line> result; // store the result here
int sum_elements_i = 0;
int left_counter = 0; // counter to iterate over the left Matrix
int right_counter = 0; // and for the right one
for(int i = 0; i < rows -1;++i){
for(int j = 0; j < other.spalte -1; ++j){
sum_elements_i = 0;
for(int k = 0; k < other.reihe -1; ++k){
sum_elements_i += local_matrix.at(left_counter) * other.local_matrix.at(right_counter);
left_counter++; // get the next value of the left matrix
right_counter += other.line; // and the next one of the right here we need to skip n-elements
// so its the 1st/2nd/.. value of the next line
}
right_counter = j+1;
result(i, j) = sum_elements_i;
}
left_counter = i+1;
}
return result;
}
};
我对*运算符的问题是,每次我想用3x3矩阵乘以4x3时,程序都会崩溃,并出现以下错误消息:
invalid operands to binary expression ('Matrix<int, 4, 3>' and 'Matrix<int, 3, 3>')
E = D*A;
~^~
这是我调用函数的代码:
Matrix<int, 3, 3> A; // Datatype, Rows, Columns
A(0,0) = 1; A(0,1) = 2; A(0,2) = 3;
A(1,0) = 1; A(1,1) = 2; A(1,2) = 3;
A(2,0) = 1; A(2,1) = 2; A(2,2) = 3;
Matrix<int, 4, 3> D, E;
D(0,0) = 1; D(0,1) = 2; D(0,2) = 3;
D(1,0) = 4; D(1,1) = 5; D(1,2) = 6;
D(2,0) = 7; D(2,1) = 8; D(2,2) = 9;
D(3,0) = 7; D(3,1) = 8; D(3,2) = 9;
std::cout << std::endl << "D:n" << D << std::endl;
E = D*A;
任何帮助都将不胜感激,谢谢。
您必须为operator *
:使用正确的矩阵大小
template <typename T, std::size_t N, std::size_t K, std::size_t M>
Matrix<T, N, M> operator* (const Matrix<T, N, K>& lhs, const Matrix<T, K, M>& rhs);
定义函数的方式是,函数具有签名Matrix<int, 4, 3> Matrix<int, 4, 3>::operator(const Matrix<int, 4, 3> &) const
,这意味着你必须尝试将一个4乘3的矩阵与一个4乘以3的矩阵相乘,它会以某种方式给你另一个4×3的矩阵。相反,你想要的,至少是将你的operator*
声明为
template <int cols2>
Matrix<int, rows, cols2> operator*(const Matrix<int, cols, cols2> & other) const {...}
例如,这将允许您将4乘3矩阵与3乘3矩阵相乘,从而正确输出4乘3的矩阵。
为了允许更多类型的矩阵相乘,您可以进行的另一个更改是使签名
template <class T2, int cols2>
Matrix<typename std::common_type<T, T2>::type, rows, cols2>
operator*(const Matrix<T2, cols, cols2> & other) const {...}
相关文章:
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 二进制表达式的操作数无效 - 使用 for 和迭代器
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- 二进制表达式的操作数无效?
- 二进制表达式的操作数无效("常量字符*"和"常量字符*")
- 没有变量声明为函数,但错误:二进制表达式的操作数无效
- 为什么映射迭代器显示基本操作数无效错误
- 如何修复此错误:二进制表达式的操作数无效("std::vector<double>"和"double")?
- 错误:二进制表达式的操作数无效(映射使用查找函数错误)
- C++:"错误:类型'const char*'和'const char [28]'的操作数无效到二进制'ope
- 错误:使用索引寻址和 Clang 的指令的操作数无效
- 错误:二进制'operator*' 'int [1]'和'float'类型的操作数无效
- 派生类的重载<<运算符表示操作数无效
- 错误:C++ 中二进制"operator%"类型"float"和"int"的操作数无效
- 实现 vector 时二进制表达式的操作数无效
- 提升multi_index示例:错误:二进制表达式的操作数无效
- 字符和常量字符[2] 类型的操作数无效到二进制运算符
- 二进制表达式的操作数无效("无线电设备"和"常量无线电设备")