C++ 谢谢你的回答,但这对我来说不太好。不,我得到了以下编译器错误:“重新定义参数‘other’”,但我猜您现在试图用三个值重载运算符,而程序选择了正确的值?我还用头文件的定义更新了我的问题-欢迎使用Matrix将int作为其大小类型,因此最好坚持使用int(除
C++ 谢谢你的回答,但这对我来说不太好。不,我得到了以下编译器错误:“重新定义参数‘other’”,但我猜您现在试图用三个值重载运算符,而程序选择了正确的值?我还用头文件的定义更新了我的问题-欢迎使用Matrix将int作为其大小类型,因此最好坚持使用int(除,c++,c++11,matrix,matrix-multiplication,C++,C++11,Matrix,Matrix Multiplication,谢谢你的回答,但这对我来说不太好。不,我得到了以下编译器错误:“重新定义参数‘other’”,但我猜您现在试图用三个值重载运算符,而程序选择了正确的值?我还用头文件的定义更新了我的问题-欢迎使用Matrix将int作为其大小类型,因此最好坚持使用int(除非您将所有内容更改为size)。另外,他当前在操作符*中的逻辑依赖于0-N在提供矩阵定义的编辑之前给出有符号的-1@M.M:回答,实际上,整个类的类型大小应该是一致的。@VicKaczm:这个操作符是一个自由函数,而不是成员函数。嘿,谢谢你的回
谢谢你的回答,但这对我来说不太好。不,我得到了以下编译器错误:“重新定义参数‘other’”,但我猜您现在试图用三个值重载运算符,而程序选择了正确的值?我还用头文件的定义更新了我的问题-欢迎使用
Matrix
将int
作为其大小类型,因此最好坚持使用int
(除非您将所有内容更改为size
)。另外,他当前在操作符*
中的逻辑依赖于0-N
在提供矩阵定义的编辑之前给出有符号的-1
@M.M:回答,实际上,整个类的类型大小应该是一致的。@VicKaczm:这个操作符是一个自由函数,而不是成员函数。嘿,谢谢你的回答,但这对我来说不太好。不,我得到了以下编译器错误:“重新定义参数‘other’”,但我猜您现在试图用三个值重载运算符,而程序选择了正确的值?我还用头文件的定义更新了我的问题-欢迎使用Matrix
将int
作为其大小类型,因此最好坚持使用int
(除非您将所有内容更改为size
)。另外,他当前在操作符*
中的逻辑依赖于0-N
在提供矩阵定义的编辑之前给出带符号的-1
@M.M:回答,实际上,整个类的类型大小应该是一致的。@VicKaczm:这个操作符是一个自由函数,不是成员函数。非常感谢,更改我函数的签名是关键!现在一切都很好:)干杯非常感谢,更改我函数的签名是关键!现在一切正常:)干杯
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;
}
};
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;
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);
template <int cols2>
Matrix<int, rows, cols2> operator*(const Matrix<int, cols, cols2> & other) const {...}
template <class T2, int cols2>
Matrix<typename std::common_type<T, T2>::type, rows, cols2>
operator*(const Matrix<T2, cols, cols2> & other) const {...}