如何在c++;? 我是C++新手,模板在语法上绝对不友好。基本上,这里是我编写、测试和完成的一些函数。仅仅是一个简单的问题,我花了几个小时为我的matrix类编写了一个非成员的操作符重载,并且一直遇到各种类型的语法错误。那么,我从哪里开始呢

如何在c++;? 我是C++新手,模板在语法上绝对不友好。基本上,这里是我编写、测试和完成的一些函数。仅仅是一个简单的问题,我花了几个小时为我的matrix类编写了一个非成员的操作符重载,并且一直遇到各种类型的语法错误。那么,我从哪里开始呢,c++,oop,templates,overloading,non-member-functions,C++,Oop,Templates,Overloading,Non Member Functions,这是我的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&l

这是我的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 &);
};
\ifndef MYMATRIX\u H
#定义MYMATRIX_H
#包括
#包括
#包括
使用名称空间std;
/*我用这个矩阵来存储数据*/
样板
使用twoD=std::vector;
样板
类矩阵{
私人:
int行;
int cols;
二维矩阵;
受保护的:
无效validSizeCheck(整数行,整数列);
公众:
矩阵(int行,int列);
矩阵(int行,int列,twodnewmatrix);
二维getMatrix();
int getRows();
int getCols();
无效打印矩阵();
void运算符=(常数矩阵&);
矩阵和运算符+=(常数矩阵和);
矩阵算子+(常数矩阵&);
矩阵算子*(常数矩阵&);
矩阵算子*(常数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;    
}
模板
矩阵运算符-(常数矩阵和lhs,常数矩阵和rhs){
如果(lhs.rows!=rhs.cols | | lhs.rows!=rhs.cols{
抛出异常();
}
矩阵tmp(左侧行,左侧列);
对于(int i=0;i
很明显,这不起作用。。 那么我应该遵循什么语法呢?同样,这可能是一个非常愚蠢的问题,但非成员函数应该在头文件还是.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);        
};
模板
类矩阵{
私人:
...
受保护的:
...
公众:
...
样板
友元矩阵算子-(常数矩阵和lhs,常数矩阵和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;    
}
模板
矩阵运算符-(常数矩阵和lhs,常数矩阵和rhs){
if(lhs.rows!=rhs.cols | | lhs.rows!=rhs.cols){
抛出异常();
}
矩阵tmp(左侧行,左侧列);
对于(int i=0;i

工作完美,无警告!感谢所有帮助

成员
是私有的。非类成员运算符必须是类的朋友。在类中声明它:

friend Matrix<T> operator-<>(const Matrix<T>& lhs,const Matrix<T>& rhs);
友元矩阵算子-(常数矩阵和lhs,常数矩阵和rhs);
阅读本文了解更多信息:

此外,这可能是一个非常愚蠢的问题,但非成员函数应该在头文件还是.cpp文件中?到目前为止,我编写的所有内容都在.h文件中,因为它们都是模板


您做得对,所有模板都必须在头文件中。以下是详细信息:

成员
是私有的。非类成员运算符必须是类的朋友。在类中声明它:

friend Matrix<T> operator-<>(const Matrix<T>& lhs,const Matrix<T>& rhs);
友元矩阵算子-(常数矩阵和lhs,常数矩阵和rhs);
阅读本文了解更多信息:

此外,这可能是一个非常愚蠢的问题,但非成员函数应该在头文件还是.cpp文件中?到目前为止,我编写的所有内容都在.h文件中,因为它们都是模板


您做得对,所有模板都必须在头文件中。以下是更多信息:

您需要在内部temp变量的声明中再次使用模板参数:

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;    
}
模板
矩阵运算符-(常数矩阵和lhs,常数矩阵和rhs){
如果(lhs.rows!=rhs.cols | | lhs.rows!=rhs.cols{
抛出异常();
}
矩阵tmp(左侧行,左侧列);
对于(int i=0;i

(更改在第5行:
矩阵tmp(lhs.rows,lhs.cols);

您需要在以下内容中的temp变量声明中再次使用模板参数:

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;    
}
模板
矩阵运算符-(常数矩阵和lhs,常数矩阵和rhs){
如果(lhs.rows!=rhs.cols | | lhs.rows!=rhs.cols{
抛出异常();
}
矩阵tmp(左侧行,左侧列);
对于(int i=0;i

(更改在第5行:
矩阵tmp(lhs.rows,lhs.cols);

不匹配的括号帮不了什么忙…让我觉得这不是你的真实代码不匹配的括号在哪里?这就是我一直在编码的方式,只要我还记得…哦,我解决了我的问题…不匹配的括号帮不了什么忙…让我觉得这不是你的真实代码不匹配的父母在哪里sis?从我记事起,这就是我一直在编码的方式…哦,我解决了我的问题…嗨,朋友,谢谢你的快速回答。不幸的是,我也尝试过,这里是我正在处理的:myMatrix。h:34:78:警告:朋友声明“矩阵运算符-(常数矩阵&,常数矩阵&)”声明了一个非模板函数[-Wnon-template-friend]友元矩阵运算符-(const-Matrix&lhs,const-Matrix&rhs);^myMatrix.h:34:78:注意:(如果这不是您想要的,请确保函数模板已经