Assigning a complex constructor through new

I have a confusion. The following is a snippet of code.

I want to create a dynamic array of five class objects using new , but I want to run a loop to assign the first constructor parameter using a loop counter. Sort of.

 class A { public: A(int _x, int _y):x(_x),y(_y) {} private: int x,y; }; int main() { A* a = new A[5]; //compiler error for(i=0;i<5;i++) { a[i] = A(i, 10); } } 

Can someone please tell me which correct syntax to do this since I don't have a simple constructor?

+4
source share
6 answers
 A* a = new A[5]; 

tries to build 5 objects 5 type A using the default constructor A , which is not in your class. You must implement it:

 class A { public: A() : x_(0), y_(10) { } // default constructor A(int x, int y) : x_(x), y_(y) { } private: int x_; int y_; }; 

but note that it would be better to use std::vector instead of this dynamically allocated array:

 std::vector<A> a(5); 

which also creates 5 type A objects using the default constructor :)

+3
source

This line

 A* a = new A[5]; 

requires A to be constructive by default. So a simple option is to add a default constructor to A :

  A(): x(), y() {} // zero-initializes x and y 

Note that in C ++ you can usually use std::vector<A> in this case. This requires all memory management, so there is no need to explicitly call new and delete . It can also be changed dynamically. This would create std::vector<A> with five default A objects built:

 std::vector<A> a(5); 

although you probably want to create an empty one and insert values ​​into it in the loop.

 std::vector<A> a; for(i=0;i<5;i++) { a.push_back(A(i, 10)); } 
+7
source

Since you do not have a constructor that does not accept any parameters, you can:

 A(int _x = 0, int _y = 0):x(_x),y(_y) {} 

or

 A() : x(0), y(0) {} 

And instead of a for loop write

 for(i=0;i<5;i++) { a[i].set(i, 10); } 

With the appropriate set method

+1
source

You have two options:

  • use A** a = new A*[5] and a[i] = new A(i, 10) , that is, say, an array of pointers to A as opposed to an array of objects.

  • Either include the default constructor A(): x(0), y(0) {} or simply add the default parameters to your existing constructor.

Your current compiler error is related to the fact that new A[5] builds 5 continuous A and allocates 5 * sizeof(A) bytes of memory for this and calls the default constructor A 5 times (the constructor without any parameters), but this The default constructor is not available in your class A.

0
source

The problem is that you do not have a default constructor. When you call a new one, it needs to somehow create objects and cannot. You must either provide one (that is, one that initializes with zeros x and y ), or use a list of initializers , if C ++ 11 is an option.

0
source

If you do not want the default constructor to be defined, try this,

 int main() { A* a[5]; for(int i=0;i<5;i++) { a[i] = new A(i, 10); } } 
0
source

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


All Articles