I am coding in c++ on ubuntu. I am trying to dynamically create a matrix. I want to know what is wrong with this code. I am getting an error on {**matrix = new int *[row];} this line - invalid conversion from int** to int. I googled a lot, and everyone has used the same syntax. Please HELP, I've been stuck here for an entire day now!
int main()
{
int **matrix;
int i,j, count;
int row;
int col;
cout << "\n Enter the number of rows and columns for the matrix.";
cin >> row >> col ;
**matrix = newint *[row];
for (count = 0; count < row; count++)
matrix[count] = newint [col];
cout << "\nNow enter the element for the matrix.";
for (i=0; i < row; i++)
{
for (j=0; j < col; j++)
{
cout << "\nRow " << (i+1) << " Col " << (j+1) << " :";
cin >> *(*(matrix + i) + j);
}
}
for (int count = 0; count < row; count++)
delete[] matrix[count];
delete[] matrix;
matrix = 0;
return 0;
}
It is worth noting that std::vector<std::vector<int>> matrix is fine for the C++11 standard, but if he is using an older standard or not specifying -std=c++11, >> will generate errors.
1 2 3
std::vector<std::vector<int>> matrix; // fine for C++11
std::vector<std::vector<int> > matrix; // for pre-C++11