简体   繁体   English

重载<<运算符以在模板类中打印矩阵的元素

[英]Overloading << operator to print the elements of a matrix in a template class

why am I having an error on the operator '[]'. 为什么操作符'[]'出错。 I wanted to print the contents of my matrix. 我想打印矩阵的内容。 If i can't use the brackets, what could i do then? 如果我无法使用括号,那该怎么办?

here's a sample of the code: 这是代码示例:

#include <iostream>
#include <vector>

template <typename T>
class Matrix {
    private:
    int m; int n;
    std::vector<T> x;
    std::vector<std::vector<int>> Mat;


    public:
    Matrix (const unsigned int &m, const unsigned int &n, std::vector<T> x);
    Matrix(const Matrix &M);
    Matrix<T> operator = (const Matrix &M);
    // Matrix<T> operator [](const int &index);

    friend std::ostream& operator << (std::ostream& os, const Matrix<T> &M) {
        os << "[";
        for (int i = 0; i< M.m; i++){
            for (int j = 0; j< M.n; j++){
                os << M[i][j] << ' ';
            }
            os << '\n';
        }
        os << "]\n";
        return os;
    }
};

I have fixed the errors. 我已经修复了错误。 But it doesn't print my matrix. 但这不会打印我的矩阵。 This is my main: 这是我的主要:

int main(){
    std::vector<int> x = {1,2,3,4};
    Matrix<int> A{2,2,x};
    Matrix<int> B{2,2,x};
    std::cout << A;
    std::cout << B;
    return 0;
}

And this is my constructor, I needed to make a matrix from a vector where I specify the rows and columns. 这是我的构造函数,我需要根据指定行和列的向量制作矩阵。

template <typename T>
    Matrix<T>::Matrix (const unsigned int &m, const unsigned int &n, std::vector<T> x){ //constructor
    this -> m = m;
    this -> n = n;
    this -> x = x;

    int index = 0;
    for (int i = 0; i<m; i++){
        for (int j = 0; j<n; j++){
             Mat[i][j] = x[index];
            index++;
        }
    }
}

The issue is this line: 问题是此行:

os << M[i][j] << ' ';

Because M is of type Matrix<T> and you haven't defined [] operator, any attempts to use [] operator will give you error. 因为M的类型为Matrix<T>并且您尚未定义[]运算符,所以任何尝试使用[]运算符都会给您带来错误。

Instead, You should use data member Mat . 相反,您应该使用数据成员Mat

friend std::ostream& operator << (std::ostream& os, const Matrix<T> &M) {
    os << "[";
    for (int i = 0; i< M.m; i++){
        for (int j = 0; j< M.n; j++){
            os << M.Mat[i][j] << ' '; // using M.Mat instead of M
        }
        os << '\n';
    }
    os << "]\n";
    return os;
}

Edit: 编辑:

As per your updated code, you may get a nice SIGSEGV error (Segmentation Fault) issue. 根据您更新的代码,您可能会遇到一个不错的SIGSEGV错误(分段错误)问题。 Why? 为什么? Simple. 简单。 You are not resizing your data member Mat . 您没有在调整数据成员Mat大小。

You have to do this: 您必须这样做:

template <typename T>
    Matrix<T>::Matrix (const unsigned int &m, const unsigned int &n, std::vector<T> x){ //constructor
    this -> m = m;
    this -> n = n;
    this -> x = x;

    int index = 0;

    // resizing Matrix according to our need i.e. m rows and n columns
    Mat.resize(m);
    for (unsigned int i = 0; i < Mat.size(); i++) {
        Mat[i].resize(n);
    }

    for (unsigned int i = 0; i<m; i++){
        for (unsigned int j = 0; j<n; j++){
             Mat[i][j] = x[index];
            index++;
        }
    }
}

You have different mistakes in your code: 您的代码中有不同的错误:

1.- Your constructor: 1.-您的构造函数:

 Matrix<T>::Matrix (const unsigned int &m, const unsigned int &n, std::vector<T> x)

it's not a good idea to pass a vector by value, its always better to pass it by reference, and never pass an integer by const reference, pass it by value: 按值传递向量不是一个好主意,按引用传递向量总是更好,并且永远不要按const引用传递整数,按值传递:

 Matrix<T>::Matrix (unsigned int m, unsigned int n, const std::vector<T>& x)

2.- This is not an error, but you don't usually need to use "this->", and its better to construct an object like: 2.-这不是一个错误,但是您通常不需要使用“ this->”,最好构造一个像这样的对象:

Matrix<T>::Matrix (unsigned int m0, unsigned int n0, const std::vector<T>& x0)
      : m{m0}, n{n0}, x{x0} {...}

3.- I don't understand why you make 2 copys of the same elements: one in your vector x, and other with M. Your wasting your memory. 3.-我不明白为什么要制作两个相同元素的副本:一个在向量x中,另一个在M中。您浪费了内存。 Maybe with x is enough? 也许用x足够了?

4.- You define m and n as signed int, but in your constructor you use "unsigned" int. 4.-您将m和n定义为signed int,但在构造函数中,您使用的是“ unsigned” int。 Which one do you want to use? 您要使用哪一个?

I propose this changes: 我建议进行以下更改:

1.- If you want to access to an element (i,j), write the operator: 1.-如果要访问元素(i,j),请编写运算符:

  Matrix& operator()(unsigned int i, unsigned int j);

its easy using your vector x. 使用向量x很容易。

2.- To write all the elements you can write something like: 2.-要编写所有元素,您可以编写如下内容:

std::ostream& operator << (std::ostream& os, const Matrix<T>& M) {
    os << "[";
    for (int i = 0; i< M.rows(); ++i){
        for (int j = 0; j< M.cols(); ++j){
            os << M(i, j) << ' ';
        }
        os << '\n';
    }
    os << "]\n";
    return os;
}

and this is not a friend function (if you define rows() and cols()). 这不是一个朋友函数(如果您定义rows()和cols())。

your operator[] returns a Matrix. 您的运算符[]返回一个矩阵。 You should change it to your template value. 您应该将其更改为模板值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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