1

我是 C++ 新手,模板在语法上绝对不友好。基本上,这是我编写、测试和完成的一些功能。只是一个简单的问题,我已经尝试了几个小时来为我的矩阵类编写一个非成员运算符重载并且已经得到所有类型的语法错误。那么,我从哪里开始呢?

这是我的 myMatrix.h:

#ifndef MYMATRIX_H
#define MYMATRIX_H

#include <exception>
#include <vector>
#include <iostream>

using namespace std;

/* I'm using this matrix to store data */
template<typename T>
using twoD = std::vector<std::vector<T>>;

template<class T>
class Matrix{
    private:
        int rows;
        int cols;
        twoD<T> matrix;
    protected:
        void validSizeCheck(int rows, int cols);
    public:
        Matrix(int rows, int cols);
        Matrix(int rows, int cols, twoD<T> newMatrix);
        twoD<T> getMatrix();
        int getRows();
        int getCols();
        void printMatrix();
        void operator=(const Matrix<T> &);
        Matrix<T> &operator+=(const Matrix<T> &);
        Matrix<T> operator+(const Matrix<T> &);
        Matrix<T> operator*(const Matrix<T> &);
        Matrix<T> operator*(const T &);
};

这是我尝试过的一件事:

template <class T>
Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){
    if(lhs.rows != rhs.cols || lhs.rows != rhs.cols{
        throw exception();
    }
    Matrix tmp(lhs.rows, lhs.cols);
    for(int i = 0; i < lhs.rows; i++){
        for(int j = 0; j < cols; j++){
            tmp.matrix[i][j] = lhs.matrix[i][j]+rhs.matrix[i][j];
        }
    }  
    return tmp;    
}

显然这不起作用..那么我应该遵循什么语法呢?此外,这可能是一个非常愚蠢的问题,但非成员函数应该在头文件还是 .cpp 文件中?到目前为止,我写的所有东西都在 .h 文件中,因为它们都是模板。

任何帮助将不胜感激。

更新:我终于让代码工作了,对于类和函数声明,我添加了这个:

template<class T>
class Matrix{
    private:
        ...
    protected:
        ...
    public:
        ...
      template<class U>
      friend Matrix<U> operator-(const Matrix<U>& lhs, const Matrix<U>& rhs);        
};

这是函数定义:

 template <class U>
 Matrix<U> operator-(const Matrix<U>& lhs,const Matrix<U>& rhs){
     if(lhs.rows != rhs.cols || lhs.rows != rhs.cols){
         throw exception();
     }
     Matrix<U> tmp(lhs.rows, lhs.cols);
     for(int i = 0; i < lhs.rows; i++){
         for(int j = 0; j < lhs.cols; j++){
             tmp.matrix[i][j] = lhs.matrix[i][j]-rhs.matrix[i][j];
         }
     }  
    return tmp;    
}

完美运行,没有警告!感谢所有帮助

4

2 回答 2

4

Members rows, cols are private. Non-class member operator must be a friend of a class. Declare it in the class:

friend Matrix<T> operator-<>(const Matrix<T>& lhs,const Matrix<T>& rhs);

Read this article for more info: Operator overloading : member function vs. non-member function?

Also, this might be a very dumb question, but should the non-member function be in the header file or the .cpp file? So far, everything that I wrote has been in the .h file as they're all templates.

You did it right, all templates must be in a header file. Here is more info: Why can templates only be implemented in the header file?

于 2018-04-29T02:30:50.437 回答
4

You need to use the template parameters again in the declaration of the temp variable inside:

template <class T>
Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){
    if(lhs.rows != rhs.cols || lhs.rows != rhs.cols{
        throw exception();
    }
    Matrix<T> tmp(lhs.rows, lhs.cols);
    for(int i = 0; i < lhs.rows; i++){
        for(int j = 0; j < cols; j++){
            tmp.matrix[i][j] = lhs.matrix[i][j]+rhs.matrix[i][j];
        }
    }  
    return tmp;    
}

(change is in Line 5: Matrix<T> tmp(lhs.rows, lhs.cols); )

于 2018-04-29T02:31:42.850 回答