[英]Why does my use of operator* fail?
I asked a question earlier yesterday and got some very good answers for sure. 我昨天早些时候问了一个问题,肯定得到了一些很好的答案。 Now I am at the end of my project and I am stuck again and can't figure out the answer.
现在我处在项目的最后,我再次陷入困境,无法找出答案。 I am going to put the most pertinent part of my code here and hope to get some insight from you all.
我将把代码中最相关的部分放在这里,并希望从大家那里获得一些见识。 The requirements are: I cannot change the code in my main.cpp, and my header file is supposed to be as simple as possible.
要求是:我无法更改main.cpp中的代码,并且我的头文件应该尽可能简单。
Having that out of the way here is the code: This is my Matrix.h file 代码如下:这是我的Matrix.h文件
#ifndef MATRIX_H
#define MATRIX_H
#include <vector>
#include <iostream>
#include <math.h>
#include <complex>
using namespace std;
namespace theMatrix {
template <typename T, size_t ROWS, size_t COLS>
class Matrix {
friend class Matrix;
public:
Matrix(const T & init = T()) : elts(ROWS, vector<T>(COLS, init)) {
};
const vector<T> & operator[](int ROWS) const {
return elts[ROWS];
};
vector<T> & operator[](int ROWS) {
return elts[ROWS];
};
//matrixMult
template<size_t INNER>
Matrix & matrixMult(const Matrix<T, ROWS, INNER> & mat1, const Matrix<T, INNER, COLS> & mat2) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
//elts[i][j] = 0;
for (int k = 0; k < INNER; k++) {
this->elts[i][j] += mat1.elts[i][k] * mat2.elts[k][j];
}
}
}
return *this;
};
//print function
void print(ostream & out) const {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
out << elts[i][j];
}
out << "\n";
}
};
private:
vector< vector<T> > elts;
};
//Operator<<
template <typename T, size_t ROWS, size_t COLS>
ostream & operator<<(ostream & out, const Matrix<T, ROWS, COLS> & elts) {
elts.print(out);
return out;
};
//Operator*
template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
Matrix<T, ROWS, COLS> returnVal;
return returnVal.matrixMult(lhs, rhs);
};
//operator matrixMult
template <typename T, size_t ROWS, size_t INNER, size_t COLS>
inline void matrixMult(const Matrix<T, ROWS, INNER> & mat1, const Matrix<T, INNER, COLS> & mat2, Matrix<T, ROWS, COLS> & mat3) {
mat3 = matrixMult(mat1, mat2);
};
This is my main.cpp: 这是我的main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // for rand()
#include "Matrix.h"
using namespace std;
using namespace theMatrix;
template <typename T, size_t ROWS, size_t COLS>
void randomize(Matrix<T, ROWS, COLS> & mat)
// Put random values in a Matrix.
// Note: It must be possible to assign T an int value.
{
for (size_t i = 0; i < ROWS; i++)
for (size_t j = 0; j < COLS; j++)
mat[i][j] = (rand() % 21) - 10; // Random number in range -10,...,+10
}
struct Complex
{
Complex(double re = 0.0, double im = 0.0) : real(re), imag(im) { }
Complex & operator+=(const Complex & rhs)
{
real += rhs.real;
imag += rhs.imag;
return *this;
}
Complex & operator-=(const Complex & rhs)
{
real -= rhs.real;
imag -= rhs.imag;
return *this;
}
Complex & operator*=(const Complex & rhs)
{
real = real * rhs.real - imag * rhs.imag;
imag = real * rhs.imag + imag * rhs.real;
return *this;
}
double real;
double imag;
};
Complex operator+(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real + rhs.real, lhs.imag + rhs.imag);
}
Complex operator-(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real - rhs.real, lhs.imag - rhs.imag);
}
Complex operator*(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real * rhs.real - lhs.imag * rhs.imag, lhs.real * rhs.imag + lhs.imag * rhs.real);
}
ostream & operator<<(ostream & out, const Complex & c)
{
out << "(" << c.real << " + " << c.imag << "i)";
return out;
}
int main()
{
// Matrix multiplication tests:
Matrix<int, 2, 3> m4;
randomize(m4);
Matrix<int, 3, 5> m5;
randomize(m5);
Matrix<int, 2, 5> m6;
Matrix<int, 2, 5> m7;
matrixMult(m4, m5, m7);
out << "m6 == m4 * m5: " << (m6 == m4 * m5) << endl; // here is the first error
// Matrices of Complex:
Matrix<Complex, 2, 8> m11;
randomize(m11);
Complex c(1, -3);
Matrix<Complex, 8, 3> m12;
randomize(m12);
out << "m11 * m12: " << endl << m11 * m12 << endl; // Here is the second error
out.close();
}
I am having only two errors which are conflicting with the complex operator * declaration which I have been trying to solve for several hours and I just can't figure it out. 我只有两个错误,它们与复杂的运算符*声明冲突,而我一直试图解决几个小时,而我却无法弄清楚。 Here are the errors:
错误如下:
Error 1 error C2678: binary '*' : no operator found which takes a left-hand operand of type 'nkumath::Matrix<T,ROWS,COLS>' (or there is no acceptable conversion)
Error 2 error C2678: binary '*' : no operator found which takes a left-hand operand of type 'nkumath::Matrix<T,ROWS,COLS>' (or there is no acceptable conversion)
Thanks again for everyone's help on this. 再次感谢大家的帮助。
EDIT: Here is the solution! 编辑:这是解决方案! which I voted for.
我投了赞成票。 Thanks!!!
谢谢!!!
//Operator*
template <typename T, size_t ROWS, size_t INNER, size_t COLS>
Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, INNER> & lhs, const Matrix<T, INNER, COLS> & rhs) {
Matrix<T, ROWS, COLS> returnVal;
return returnVal.matrixMult(lhs, rhs);
};
模板operator *
重载不允许两个输入矩阵的大小不同。
Your problem is that the templated operator*
requires both sides of the multiplication to be the same Matrix instantiation. 您的问题是,模板化
operator*
要求乘法的两面都必须是相同的 Matrix实例化。 T
, ROWS
and COLS
can't be deduced to different values for the type of lhs
and rhs
in the same function. 对于同一函数中的
lhs
和rhs
类型,不能将T
, ROWS
和COLS
推导为不同的值。
template <typename T, size_t ROWS, size_t COLS> Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs);
Matrix<int, 2, 3> m4;
Matrix<int, 3, 5> m5;
m4*m5;
If the compiler deduces ROWS
and COLS
as 2 and 3 for the above multiplication, the rhs
type won't match your templated operator*. 如果编译器针对上述乘法将
ROWS
和COLS
推导为2和3,则rhs
类型将与模板化运算符*不匹配。 And if the compiler deduces ROWS
as 3 and COLS
as 5, the lhs
type won't match. 如果编译器将
ROWS
推导为3,将COLS
推导为5,则lhs
类型将不匹配。
You'll need to define how multiplication of differently sized Matrix instances should work, and make eg: 您需要定义大小不同的Matrix实例的乘法应如何工作,并进行例如:
template <typename T, size_t ROWS_L, size_t COLS_L, size_t ROWS_R, size_t COLS_R>
Matrix<T, ROWS_L, COLS_L> operator*(const Matrix<T, ROWS_L, COLS_L> & lhs, const Matrix<T, ROWS_R, COLS_R> & rhs);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.