c++ - 为什么我对 operator* 的使用失败了?

标签 c++ visual-c++

我昨天早些时候问了一个问题,肯定得到了一些非常好的答案。 现在我的项目快结束了,我又卡住了,想不出答案。 我将把我的代码中最相关的部分放在这里,希望能从你们那里得到一些见解。 要求是:我不能更改我的 main.cpp 中的代码,并且我的头文件应该尽可能简单。

这里的代码是:这是我的 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);
};

这是我的 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();
}

我只有两个错误与复杂的运算符 * 声明相冲突,我已经尝试解决了几个小时,但我就是想不通。 以下是错误:

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)               

再次感谢大家对此的帮助。

编辑:这是解决方案!我投了赞成票。谢谢!!!

//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 * 重载不允许两个输入矩阵的大小不同。

关于c++ - 为什么我对 operator* 的使用失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5571476/

相关文章:

c++ - __cplusplus 扩展最小值

C++,获取函数名

c++ - 在MFC中捕获鼠标指针形状变化事件

c++ - 如何从 Windows 平台中的 C++ 应用程序在远程服务器上运行远程服务器的 .exe 二进制文件

visual-studio-2010 - PuTTY 源中的 VS2010 构建错误

c++ - 使用 strtok_s 分割字符串

c++ - 如何从多重 map 中删除项目

c++ - 将信号连接到 Qt 中的插槽时出现问题

c++ - 为什么没有提供 std::regex_traits<char32_t> 的定义(因此没有提供 std::basic_regex<char32_t>)?

c++ - Nicolai Josuttis 在他的书中说,开放成员函数不会清除状态标志。这不是我在 VS2010 中发现的。这是微软的问题吗?