'expression must be a modifiable lvalue c++ error

#include<iostream>
#include<array>
#include <algorithm>
using namespace std;

class Array{
    //declaring a array
    public: int a[4];
    public: int num;
    public:
    Array() {
        //initializing a array
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    a[3] = 4;
    }
    Array(const Array &NewObj){
      a = new int[4];

    for (int i = 0; i < 4; i++)
        a[i] = NewObj.a[i];
    }
};  

int main()
{
    cout<<" DEEP COPY : \n";
    cout<<"============ \n";
    //creating first object
    Array obj1;
    //changing value of a array at index 3
    obj1.a[3]=15;
    //declaring second object which is mapped by object 1
    Array obj2(obj1);
    
    
 cout << "Object 1: \n";
    //printing values of a array
    for(int i=0;i < (sizeof(obj1.a) / sizeof(obj1.a[0])); i++){
        cout<<obj1.a[i];
        cout << "\n";
        }
     cout << "Object 2: \n";
    for(int i=0;i < (sizeof(obj2.a) / sizeof(obj2.a[0]));i++){
        cout<<obj2.a[i];
         cout << "\n";
    }
    return 0;
}

when creating a deep copy of a variable :

 a = new int[4];

I received the error: expression must be a modifiable lvalue at this line.

The primary program's purpose is to make a deep copy of an array.

I started by defining the class and declaring an array.

Then I wrote a constructor () { [native code] } that initialises an array.

Then I wrote a copy constructor () { [native code] } in which I built a new array in which the values could be saved, but it failed with an exception about editable values.



Solution 1:[1]

You can get rid of a = new int[4]. Think about why you don't need that line in the regular constructor. Do you actually need to be allocating more memory?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 maxrzaw