C++中返回二维数组GridArray的问题求助
GridArray in C++ Hey there! Let's work through how to properly return that 2D GridArray in your MapEditor code. You’re spot-on to suspect the method’s return type is the issue—C++ has specific rules around multi-dimensional arrays, but we’ve got a few straightforward fixes to get you sorted.
First, let’s recap your setup: you have a static 2D array static int GridArray[15][20]; and need a way to access it outside the MapEditor class. Here are the best approaches:
1. Return a Pointer to the Array (Works with Your Current Raw Array)
Since GridArray is static, its lifetime lasts the entire program, so returning a pointer to it is safe. The correct return type for a pointer to a 20-element int array is int (*)[20]. Here’s how to write a dedicated getter method:
// Inside your MapEditor class declaration class MapEditor { static int GridArray[15][20]; public: // Getter for GridArray int (*getGrid())[20]; // ... your existing updateGrid method ... }; // Implementation of the getter int (*MapEditor::getGrid())[20] { return GridArray; }
You can use it like this:
MapEditor editor; int (*grid)[20] = editor.getGrid(); // Access elements with grid[row][col], e.g., grid[0][0] = 5;
That int (*)[20] syntax looks odd at first, but it just means "a pointer to an array of 20 integers"—which perfectly matches each row in your 15x20 grid.
2. Use typedef to Simplify the Type
If that pointer syntax feels clunky, use a typedef to give the row type a readable name:
// Define the row type (can go inside the class or globally) typedef int GridRow[20]; class MapEditor { static GridRow GridArray[15]; public: GridRow* getGrid(); // ... other methods ... }; // Getter implementation GridRow* MapEditor::getGrid() { return GridArray; }
Usage stays intuitive:
GridRow* grid = editor.getGrid(); int tileValue = grid[3][7]; // Access row 3, column 7
3. Modern C++: Use std::array (Recommended)
If you’re using C++11 or later, swapping the raw array for std::array makes the code safer, more readable, and lets you return a reference instead of a pointer:
First, update your static array definition (don’t forget the header!):
#include <array> class MapEditor { static std::array<std::array<int, 20>, 15> GridArray; public: std::array<std::array<int, 20>, 15>& getGrid(); // ... your updateGrid method ... }; // Initialize the static array (empty braces set all values to 0) std::array<std::array<int, 20>, 15> MapEditor::GridArray{}; // Getter implementation std::array<std::array<int, 20>, 15>& MapEditor::getGrid() { return GridArray; }
Using this is straightforward, with clear type information:
auto& grid = editor.getGrid(); grid[2][5] = 255; // Set row 2, column 5 to 255
Quick Note on Your updateGrid Method
Right now, updateGrid returns an int, which doesn’t align with your goal of returning the grid. It’s better to split responsibilities: let updateGrid handle cursor movement and drawing (like it’s doing now), and use the separate getGrid method to return the array. This keeps your code organized and each method focused on one task.
内容的提问来源于stack exchange,提问作者Alexanderp

