How can I initialize an array of elements of objects that do not have a standard constuctor?

I was wondering if anyone could tell me how I should initialize an array of elements due to the fact that the object does not have a default constructor (i.e. requires parameters).

For instance:

class Foo { public: Foo() : memberArray{bar(1), bar(3), bar(2)} // **The compiler doesnt like this** {} private: Bar memberArray[3]; }; struct Bar { Bar(std::int32_t param1) { } } 

I am using GCC 4.6.1 and compiling for C ++ 11. Can someone please indicate where I am going wrong? (BTW, please do not offer dynamically allocated memory, since I do not have it.)

-1
source share
1 answer

You have a few problems: inconsistent case, use before declaration, missing semicolons, missing. This is much closer:

 struct Bar { Bar(int param1) { } }; class Foo { public: Foo() : memberArray{Bar(1), Bar(3), Bar(2)} {} private: Bar memberArray[3]; }; 
+2
source

Source: https://habr.com/ru/post/1388159/


All Articles