简体   繁体   中英

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.

Having that out of the way here is the code: This is my Matrix.h file

#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:

#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. T , ROWS and COLS can't be deduced to different values for the type of lhs and rhs in the same function.

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*. And if the compiler deduces ROWS as 3 and COLS as 5, the lhs type won't match.

You'll need to define how multiplication of differently sized Matrix instances should work, and make eg:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM