I am facing a problem in initializing an array of struct. Below is the code:
#include <iostream>
#include <array>
#include <string>
#define NUM_ELEMENT 5
struct Person
{
std::string m_name;
int m_age = 0;
Person() = default;
Person(std::string name, int age)
: m_name(name), m_age(age) {}
};
typedef std::array<Person, NUM_ELEMENT> PersonList;
class Detail
{
public:
void InitializePerson();
private:
PersonList personList;
};
void Detail::InitializePerson()
{
personList{ // <------ Getting Error here..
Person("abc", 10),
Person("cde", 20),
Person("pqr", 30),
Person("xyz", 40),
Person("apple", 50),
};
}
int main()
{
Detail detail;
detail.InitializePerson();
return 0;
}
Though, I know I can use the std::vector with push_back
but I want to achive this through the static array as it elements are fixed. I want to initialize the array with above class Detail member and since the data can be random, so not able to do in for loop
by personList[0] = Person{};
Aucun commentaire:
Enregistrer un commentaire