In function "int main()': error:could not convert '{{"LittleDog",8},{"LittleCat",3},{"LittleLion",5}}' from "<brace-enclosed initializer list>' to ‘std::vector<Animal>' vector <Animal> animalList={{"LittleDog",8},{"LittleCat",3},{"LittleLion",5}}; how to fix this c++ code error?
时间: 2023-07-05 12:08:26 浏览: 75
The error message indicates that you are trying to initialize a `std::vector<Animal>` with a brace-enclosed initializer list, but the conversion is not possible.
To fix the error, you can use the `std::initializer_list` constructor of `std::vector` to initialize the vector with the given elements. Here's an example:
```
#include <vector>
#include <string>
struct Animal {
std::string name;
int age;
};
int main() {
std::vector<Animal> animalList = {
{"LittleDog", 8},
{"LittleCat", 3},
{"LittleLion", 5}
};
// ...
}
```
This code will create a vector of `Animal` objects and initialize it with the given elements using the `std::initializer_list` constructor.
阅读全文