error: expected primary-expression before ‘]’ token Index[] = { {0,A},{1,B},{2,C} };
时间: 2024-05-05 14:17:25 浏览: 95
This error occurs when the compiler encounters a closing square bracket ']' without finding the corresponding opening square bracket '[' earlier in the code.
In this particular example, the error message suggests that there is an issue with the declaration or initialization of an array called "Index". The code snippet provided shows that the array is being initialized with a list of three elements, each of which is a pair of values enclosed in curly braces.
However, it seems that the compiler is unable to recognize the type of the elements in the array, possibly because the data types for "A", "B", and "C" have not been defined or declared yet.
To fix this error, make sure that the data types of the variables are declared before using them to initialize the array. Alternatively, you can explicitly specify the data type of the array elements by adding it before the array name, like so:
```c++
struct Pair {
int num;
char letter;
};
Pair Index[] = { {0,'A'},{1,'B'},{2,'C'} }; // explicitly specifying data type
```
Or, if you prefer to use auto keyword:
```c++
auto Index = std::vector<std::pair<int,char>>{{0,'A'},{1,'B'},{2,'C'}};
```
This will tell the compiler that each element in the array is a pair consisting of an integer and a character.
阅读全文