简体   繁体   中英

Printing a dynamic 2D array, C++

I'm attempting to print the contents of a dynamically allocated 2D array. However, when I try to subscript an element I get an error. Any help would be appreciated.

The snippet that makes the array:

   char ** m_points = new char * [m_height];

   for (int y_i=0; y_i<m_height; y_i++)
   {
       m_points[y_i] = new char[m_width];

       for (int x_i=0; x_i<m_width; x_i++)
           m_points[y_i][x_i] = ' ';   
   }

This is supposed to print the arrays contents ( cout << m_points[y_i][x_i] is where the error is occurring):

   for (int y_i=0; y_i<m_height; y_i++)
   {       
       for (int x_i=0; x_i<m_width; x_i++)
           cout << m_points[y_i][x_i];

       cout << endl;
   }

Error:

invalid types ‘char[int]’ for array subscript

Well I feel like an idiot now, LOL. m_points was a member variable, and I declared it twice (once in the constructor, and once in the header file). After amending this issue, everything works perfectly. Thanks for the help, anyways.

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