C++ 操作员<&书信电报;c+中的重载+;

C++ 操作员<&书信电报;c+中的重载+;,c++,matrix,operator-overloading,C++,Matrix,Operator Overloading,我做了这个密码 声明: template <class T> class Matrix { std::vector< std::vector<T> > mat; size_t rows , cols; public: Matrix<T>(); Matrix<T>(const std::string); Matrix(const size_t r, const size_t c, const st

我做了这个密码

声明:

template <class T>
class Matrix
{
    std::vector< std::vector<T> > mat;
    size_t rows , cols;
public:

    Matrix<T>();
    Matrix<T>(const std::string);
    Matrix(const size_t r, const size_t c, const std::vector<T> v);
    Matrix<T> operator=(const Matrix<T> &other);

    friend std::ostream& operator<<(std::ostream &os , Matrix<T> m);
};
模板
类矩阵
{
std::vectormat;
行大小,列数;
公众:
矩阵();
矩阵(const std::string);
矩阵(常数大小r,常数大小c,常数标准::向量v);
矩阵运算符=(常数矩阵和其他);
friend std::ostream和操作员
当然,除非您可以显式地实例化所有需要的专门化

无论如何,考虑一下:

模板
类矩阵
{
//...

friend std::ostream&operator是.h文件中的定义,它是
#包含
main.cpp中的
d
?发布一条消息将非常有用。我不知道这是否与您的问题有关(我不认为是这样的)但是您仍然应该让您的函数使用
常量矩阵&
而不是
矩阵
@IgorGumush在头文件中直接定义模板运算符,而不是在.cpp文件中,因为这是一个模板,
运算符的定义
template <class T>
std::ostream& operator<<(std::ostream &os , Matrix<T> m){

    for (size_t i = 0; i < m.rows; i++)
    {
        for (size_t j = 0; j < m.cols; j++)
        {
            os << m.mat[i][j] << " ";
        }
        os << std::endl;
    }
    return os;
}
int main(){
    std::vector<int> v(9);
    v[0] = 1;
    v[1] = 2;
    v[2] = 3;
    v[3] = 4;
    v[4] = 5;
    v[5] = 6;
    v[6] = 7;
    v[7] = 8;
    v[8] = 9;
    Matrix<int> m = Matrix<int>(2, 3, v);
    std::cout << m;
}
template <class T>
class Matrix
{
    //...
    friend std::ostream& operator<<(std::ostream &os , Matrix<T> m) {
        // do things
        return os;
    }
};