c++ - Reference object array outside of function it is declared in -


int main() {     tile map[50][50];      ///// generate map \\\\\      buildroom(10, 10, 10, 10, 1);      return 0; }  void buildroom(int startx, int starty, int sizex, int sizey, int direction) {     if (direction == 1)     {         (int x; x++; x > sizex)             map[startx + x][starty].type = 1;     } } 

currently map variable doesn't exist in context of buildroom() function. how able access map variable outside of main() function?

pass parameter. (arrays, unlike other parameters, not copied when passed value. modifying parameter alter external array).

int main() {     tile map[50][50];      ///// generate map \\\\\     buildroom(map, 10, 10, 10, 10, 1); }  void buildroom(tile map[50][50], int startx, int starty, int sizex, int sizey, int direction) {     if (direction == 1)     {         (int x; x++; x > sizex)              map[startx + x][starty].type = 1;     } } 

or put in class.

#include <cassert>  class map { public:     static const int mapsizex = 50;     static const int mapsizey = 50;      void buildroom(int startx, int starty, int sizex, int sizey, int direction)     {         if (direction == 1)         {             (int x; x++; x > sizex)                  gettile(startx + x, starty).type = 1;         }     }      const tile& gettile(int x, int y) const     {         assert(x > 0 && x < mapsizex && y > 0 && y < mapsizey);         return data[x][y];     }      tile& gettile(int x, int y)     {         assert(x > 0 && x < mapsizex && y > 0 && y < mapsizey);         return data[x][y];     }  private:     tile data[mapsizex][mapsizey]; }  int main() {     map mymap;      ///// generate map \\\\\     mymap.buildroom(10, 10, 10, 10, 1); } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -