简体   繁体   中英

Vector subscript out of range Visual Studio C++

I'm working with matrices in C++. The task is to find odd numbers in a matrix. I created a function, but when executed with Debug > Start Without Debugging... I get the error:

Debug assertion failed. Expression: vector subscript out of range.

This is my code:

File MatrixVec.h:

#include "MatrixVec.h"
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

MatrixVec::MatrixVec(int rows, int cols, int range)
{

    row.assign(cols, 0);
    mat.assign(rows, row);

    for (int i = 0; i < mat.size(); i++) {
        for (int j = 0; j < mat[i].size(); j++){
            mat[i][j] = rand() % range;
        }
    }
}

void MatrixVec::process()
{

    int *oddNum = new int(mat[0].size());
    for (int i = 0; i < mat[0].size(); i++) {
        oddNum[i] = 0;
    }
    for (int i = 0; i < mat.size(); i++) {
        for (int j = 0; j < mat[0].size(); j++) {
            oddNum[j] += mat[i][j] % 2;
        }
    }

    for (int i = 0; i < mat[0].size(); i++) {
        cout << i + 1 << ". kolona " << oddNum[i]<< " neparnih elemenata." << endl;
    }
//Edit
delete[] oddNum; //forgot this line
}

File MatrixVec.h:

#include "Matrix.h"
#include <vector>

class MatrixVec : public Matrix {

public:
    MatrixVec(int rows, int cols, int range);
    void print();
    void process();

private:
    std::vector<int> row;
    std::vector<std::vector<int> > mat;
};

File Matrix.h:

class Matrix {

public:
    virtual void print() = 0;
    virtual void process() = 0;

};

File main.cpp

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

#include "Matrix1D.h"
#include "Matrix2D.h"
#include "MatrixVec.h"

using namespace std;

#define NUMBER_RANGE 10

int main(int argc, char* argv[])
{
   if (argc != 3)
    {
        cout << "Niste uneli potrebne argumente za pokretanje programa!"   << endl;
        cout << "Argumenti komandne linije treba da budu:" << endl;
        cout << "1. N dimenzija matrice" << endl;
        cout << "2. M dimenzija matrice" << endl;
        exit(-1);
    }

    // inicijalizacija generatora nasumičnih brojeva
    srand(unsigned int(time(NULL)));

    int rowNum = atoi(argv[1]);
    int colNum = atoi(argv[2]);

    // a)
    cout << endl << endl << "a) Matrix 1D representation" << endl;

    Matrix1D mat1(rowNum, colNum, 10);

    mat1.print();

    mat1.process();

    //b)
    cout << endl << endl << "b) Matrix 2D representation" << endl;


    Matrix2D mat2(rowNum, colNum, 10);

    mat2.print();

    mat2.process();

    //c)
    cout << endl << endl << "c) Matrix vector of vector representation" << endl;


    MatrixVec mat3(rowNum, colNum, 10);

    mat3.print();

    mat3.process();

    return 0;
}

This line int *oddNum = new int(mat[0].size()); actually creating one int with value of mat[0].size() . You want something like this to create an array of size mat[0].size() .

int *oddNum = new int[mat[0].size()];

Note: This still not answer the exact error. Seems like your mat vector is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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