Home > Enterprise >  Hey so new to cpp. I don't understand why this does't output anything
Hey so new to cpp. I don't understand why this does't output anything

Time:04-29

#include <iostream>
#include <vector>


using std::vector;
using MATRIX = vector<vector<int>>;


class Matrix {
public:
    MATRIX matrix;
    int row = matrix.size();
    int col = matrix[0].size();

    void repr() {
        for (const vector<int> &sub_arr: matrix) {
            for (int element: sub_arr) {
                std::cout << element << ' ';
            }
            std::cout << '\n';
        }
    }

    Matrix operator (const Matrix &other) {
        std::cout << row << col;
        Matrix new_matrix;

        if (row != other.matrix.size() || col != other.matrix[0].size()) { return {{}}; }

        for (int i = 0; i < row;   i) {
            vector<int> temp_arr;
            for (int j = 0; j < col;   j) {
                temp_arr.push_back(matrix[i][j]   other.matrix[i][j]);
            }
            new_matrix.matrix.push_back(temp_arr);
        }
        return new_matrix;
    }

    Matrix operator-(const Matrix &other) {
        Matrix new_matrix;
        if (row != other.matrix.size() || col != other.matrix[0].size()) { return {{}}; }

        for (int i = 0; i < row;   i) {
            vector<int> temp_arr;
            for (int j = 0; j < col;   j) {
                temp_arr.push_back(matrix[i][j] - matrix[i][j]);
            }
            new_matrix.matrix.push_back(temp_arr);
        }
        return new_matrix;
    }

//    Matrix operator*(const Matrix &other) {}
};

int main() {
    Matrix matrix = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}};
    Matrix addition = matrix   matrix;
    addition.repr();
}

So when I define row and col at the class level, it doesn't produce any output. But when I define it in the function, it works fine.

I don't understand why it won't work when I define it in the class level. I inserted some debug print statements, and row and col seem to be correct.

CodePudding user response:

The most "natural" solution I can see is to have a constructor taking the matrix data as an argument, and use that as the base for initializations:

class Matrix {
    size_t row;
    size_t col;
    MATRIX matrix;

public:
    Matrix()
        : row{ }, col{ }, matrix{ }
    {
    }

    Matrix(MATRIX const& data)
        : row{ data.size() }, col{ data.empty() ? 0 : data[0].size() }, matrix{ data }
    {
    }

    // ...
};

Note the check that the passed data isn't empty.

CodePudding user response:

When you instantiate your Matrix object like this:

Matrix matrix = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}};

the aggregate initialization takes place, first your MATRIX matrix; is initialized, then you assign values 3 and 3 to your row and col. However, in your second line of main and inside Matrix operator (const Matrix &other) you create default constructed Matrix and here you have undefined behavior, because:

    int row = matrix.size();  // 0
    int col = matrix[0].size();  // UB, out of bounds access

So, for you maybe sometimes it prints something, on other machines it might crash before the addition.

CodePudding user response:

When row and col are initialized, matrix is still empty. To initialize them after matrix is known, write a constructor for your class.

#include <vector>

using std::vector;
using MATRIX = vector<vector<int>>;

class Matrix {
public:
    MATRIX matrix;
    int row, col;

    // Constructor
    Matrix(const MATRIX& m)
        : matrix(m), row(m.size()), col(m[0].size())
    {}
};

int main() {
    Matrix matrix = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}};
}
  •  Tags:  
  • c
  • Related