I keep getting a "vector subscript out of range" error and I can't figure out why

183 Views Asked by At

I have an assignment where I have to read from a text file two matrices and two integers indicating the number of rows and columns they each have, add them together, then write them to a new text file. I have to do this by overloading both the + and << operators, as that's what we went over in class before this was assigned. I don't know if it's the get or set method I wrote for the values of the 2d vector, but no matter what I try I always get the "vector subscript out of range" error.

Input file: 3 5

1 2 3 4 5 10 8 6 4 2 -5 5 -1 1 0

9 7 5 3 1 11 11 11 11 11 6 5 6 5 6

Matrix.h:

#define MATRIX_H

#include <iostream>
#include <vector>

class Matrix {
public:
    //constructor
    Matrix(unsigned int initial_rows = 0, unsigned int initial_columns = 0) :
        num_of_rows(initial_rows), num_of_columns(initial_columns) {
        if (num_of_rows * num_of_columns > 0)
        {
            data.resize(num_of_rows, std::vector<int>(num_of_columns));
        }
    }
    
    //operators
    Matrix operator + (const Matrix&) const;
    friend std::ostream& operator << (const std::ostream&, const Matrix&);

    //getters
    unsigned int get_num_of_rows() const;
    unsigned int get_num_of_columns() const;

    //class member functions
    int get(unsigned int i, unsigned int j) const;
    void set(unsigned int i, unsigned int j, int val);

private:
    //data fields
    unsigned int num_of_rows;
    unsigned int num_of_columns;
    std::vector<std::vector<int>> data;
};
#endif

Class Source:


//getters
unsigned int Matrix::get_num_of_rows() const { return num_of_rows; }
unsigned int Matrix::get_num_of_columns() const { return num_of_columns; }

/** returns value stored at row index i and column index j
*   @param i: row index i
*   @param j: column index j
*   @returns: value at row index i and column index j
*/
int Matrix::get(unsigned int i, unsigned int j) const {
    return data[i][j];
}

/** assigns val with the integer found at data[i][j]
*   @param i: row index i
*   @param j: column index j
*   @param val: integer found at [i][j]
*/
void Matrix::set(unsigned int i, unsigned int j, int val) {
    for (i = 0; i < num_of_rows; i++) {
        for (j = 0; j < num_of_columns; j++) {
            val = data[i][j];
        }
    }
}

/** Overloads addition operator for "Matrix + Matrix"
*   @param other: second operand
*   @returns: sum of two matrices
*/
Matrix Matrix::operator + (const Matrix& other) const {
    Matrix result;
    if (num_of_rows == other.num_of_rows && num_of_columns == other.num_of_columns) {
        result.data[num_of_rows][num_of_columns] = data[num_of_rows][num_of_columns] + other.data[num_of_rows][num_of_columns];
    }
    return result;
}

/** Overloads insertion operator for "Matrix
*   @param: istream
*   @param: Matrix object
*/
std::ostream& operator << (std::ostream out, const Matrix& matrix) {
    return out << matrix.get_num_of_rows() << matrix.get_num_of_columns() << std::endl;
    for (unsigned int i; i < matrix.get_num_of_rows(); i++) {
        for (unsigned int j; j < matrix.get_num_of_columns(); i++) {
            return out << matrix.get(i, j);
        }
        out << std::endl;
    }
}

Main.cpp:

#include <fstream>
#include <string>

int main() {
    //open input file
    std::ifstream fin("matrices_1.txt");
    if (!fin) {
        std::cout << "Input file not found" << std::endl;
        return -1;
    }

    //open output file
    std::ofstream fout("result.txt");

    //declare variables
    Matrix matrix1;
    Matrix matrix2;
    unsigned int numRows;
    unsigned int numColumns;
    int value;

    //read number of rows and columns
    fin >> numRows >> numColumns;
    std::string line;
    getline(fin, line);
    std::vector<std::vector<unsigned int>> data(numRows, std::vector<unsigned int>(numColumns));

    //read dimenstions of first matrix
    for (unsigned int i = 0; i < numRows; i++) {
        for (unsigned int j = 0; j < numColumns; j++) {
            fin >> value;
            matrix1.set(i, j, value);
            std::cout << matrix1.get(i, j);
        }
        std::cout << std::endl;
    }

    
    //read dimensions of second matrix
    getline(fin, line);
    for (unsigned int i = 0; i < numRows; i++) {
        for (unsigned int j = 0; j < numColumns; j++) {
            fin >> value;
            //matrix2.set(i, j, value);
        }
    }

    //add matrices
    Matrix result = matrix1 + matrix2;

    //print result to output file
    fout << result;

    //close input and output files
    fin.close();
    fout.close();

    return 0;
}
0

There are 0 best solutions below