C++类中声明二维数组并在构造函数初始化的技术问题
Alright, let's break down the issues with your code and fix them properly. You're hitting two core C++ rules here when dealing with member arrays in a class:
1. The Problem with Your Current Code
First, let's call out the key mistakes:
- Invalid array declaration: In C++, you can't declare a 2D array like
int map[][]— you must specify the size of all dimensions except possibly the first. The compiler needs clear dimensions to calculate memory allocation and array indexing. - Illegal array assignment: You can't directly assign an initializer list like
{ {...}, {...} }to an array after it's been declared (which happens when the TForm1 instance is created). Raw arrays don't support this kind of assignment syntax.
2. Solution 1: Fixed-Size 2D Array
If you know the exact dimensions of your map upfront (let's assume 15 columns, matching your first row of 15 elements, and 10 rows for example), here's how to fix it:
Step 1: Update the Header File
Declare the array with explicit dimensions:
class TForm1 : public TForm { private: // ... your other private members ... public: __fastcall TForm1(TComponent* Owner); // Specify fixed dimensions: [number_of_rows][number_of_columns] int map[10][15]; };
Step 2: Initialize in the Constructor
Since direct assignment isn't allowed, use one of these approaches:
Option A: Loop to Copy Values
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // Define your initial map data int initialMap[10][15] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, // Add the rest of your rows here {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; // Copy each element to the member array for (int i = 0; i < 10; ++i) { for (int j = 0; j < 15; ++j) { map[i][j] = initialMap[i][j]; } } }
Option B: Use std::copy (C++11+)
For a more concise approach:
#include <algorithm> // Required for std::copy __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { int initialMap[10][15] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, // Rest of the rows... }; for (int i = 0; i < 10; ++i) { std::copy(std::begin(initialMap[i]), std::end(initialMap[i]), std::begin(map[i])); } }
3. Solution 2: Use std::vector<std::vector<int>> (Flexible Size)
If your map size might change later, or you want simpler initialization, use a nested vector instead of a raw array. This is the more modern, flexible approach:
Step 1: Update the Header File
#include <vector> // Add this include at the top of your header class TForm1 : public TForm { private: // ... your other private members ... public: __fastcall TForm1(TComponent* Owner); std::vector<std::vector<int>> map; };
Step 2: Initialize in the Constructor
Now you can directly assign the initializer list with no extra hoops:
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // Initialize the vector with your map data directly map = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, // Add all your rows here {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; }
This approach avoids raw array limitations and makes resizing or modifying the map later much easier.
内容的提问来源于stack exchange,提问作者Samy Dressel

