I have problem with this code:
1) Segmentation fault in cpp object.
The same code working on main
function.
// Create
vector< vector<int> > vec(4, vector<int>(4));
// Write
vec[2][3] = 10;
// Read
int a = vec[2][3];
I create a two-dimensional array element. There is a problem with memory allocation. I do not know how to handle it.
#include <iostream>
#include <vector>
using namespace std;
class Matrix
{
public:
int m, n;
Matrix();
Matrix(int, int);
vector<vector<int> > vec;
};
int main()
{
Matrix obj(3, 4);
obj.vec[1][2] = 33;
return 0;
}
Matrix::Matrix()
{
n = 0;
m = 0;
vector< vector<int> > vec(m, vector<int>(n));
}
Matrix::Matrix(int m, int n)
{
this->m = m;
this->n = n;
vector< vector<int> > vec(m, vector<int>(n));
}