oop - Segmentation fault when creating object c++ -


i have class in c++, has constructor in matrix.h:

private: int col; int row; bool isvalid; int **matrix;  public: matrix(int inputint); 

and in matrix.cpp file :

matrix::matrix(int inputint) {  row=inputint;  col=inputint;  (int i=0;i<row;i++)      matrix[i]=new int [col];  (int i=0;i<row;i++)      (int j=0;j<col;j++)           matrix[i][j]=0;  isvalid=true; } 

and in main.cpp wrote

matrix a(3); 

and got segmentation fault. why ?

matrix pointer pointer, need new matrix @ first.

matrix::matrix(int inputint) {  row=inputint;  col=inputint;  matrix = new int* [row];  (int i=0;i<row;i++)      matrix[i]=new int [col];  (int i=0;i<row;i++)      (int j=0;j<col;j++)           matrix[i][j]=0;  isvalid=true; } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -