• Need help with member function w/dynamic array
    4 replies, posted
I've created a dynamic array to use as a variably-sized game board, but obviously need to use it outside the function I'm using to create it itself. Can I have some assistance with turning MakeArray() into a member function? I'm getting "error C2065: 'tiles' : undeclared identifier" at tiles[i] in DrawBoard(). I think everything relevant's included. I'll keep trying myself in the meantime. Board.cpp [code]// makes the dynamic array for the board's grid of tiles. void Board::MakeArray(void) { int *tiles = NULL; tiles = new int[boardX*boardY]; for (int i=0; i < boardX*boardY; i++) { tiles[i] = 0; // initialise all board tiles to 0 } } void Board::DrawBoard(void) { for (int i=0; i < boardX*boardY; i++) { cout << tiles[i] <<" ,"; } }[/code] main.cpp [code]int main() { Board brd; //... brd.MakeArray(); brd.DrawBoard(); return 0; }[/code] Board.h [code]class Board { public: int boardX; int boardY; Board(void); ~Board(void); void MakeArray(); void DrawBoard(); //... };[/code]
for future refernece, [url]http://www.facepunch.com/threads/1053117-What-do-you-need-help-with-v2[/url] You have to make your variable, tiles, a member of class Board, like you have boardX and boardY, but make it a private member. Edit: when you do that, also remove your declaration(int *tiles = NULL and put tiles = NULL in your constructor)
I've succeeded in creating more errors than I had to begin with. :saddowns: Thanks for that extra info in the edit though; that would've no doubt created more hair-tearing for me otherwise. [editline]29th April 2011[/editline] "You have to make your variable, tiles, a member of class Board" This is exactly the thing I've been having an issue with. I know I need to do it, it's just the implementation itself I'm stuck on. Elaboration on how, in this specific instance very much appreciated.
Just declare "int *tiles" in the class instead of as a local variable in the MakeArray() function. The same way you declared boardX and boardY (except that tiles should be private).
[code]while(true) { std::cout << "Thank you so much! "; }[/code]
Sorry, you need to Log In to post a reply to this thread.