0

I have an object called Grid which is evolving in my main script. This is how I have defined Grid:

class Grid{

public:
    std::vector <Polymer> PolymersInGrid;                 // all the polymers in the grid 
    std::vector <Particle> SolventInGrid;                 // solvent molecules in the grid 
    const int x;                                          // length of x-edge of grid 
    const int y;                                          // length of y-edge of grid 
    const int z;                                          // length of z-edge of grid 
    const double kT;                                      // energy factor 
    const double Emm ;                                    // monomer-monomer interaction 
    const double Ess ;                                    // solvent-solvent interaction 
    const double Ems_n ;                                  // monomer-solvent when Not aligned 
    const double Ems_a ;                                  // monomer-solvent when Aligned

    double Energy; 
    std::map <std::vector <int>, Particle> OccupancyMap;     // a map that gives the particle given the location
    

    Grid(int xlen, int ylen, int zlen, double kT_, double Emm_, double Ess_, double Ems_n_, double Ems_a_): x (xlen), y (ylen), z (zlen), kT (kT_), Emm(Emm_), Ess (Ess_), Ems_n (Ems_n_), Ems_a (Ems_a_) {        // Constructor of class
        // this->instantiateOccupancyMap(); 
    };


    // Destructor of class 
    ~Grid(){                                    

    }; 
...
};

In my main script, my Grid evolves when I apply an operator to Grid.

    Grid G = CreateGridObject(positions, topology);

    std::cout << "Energy of box is: " << G.Energy << std::endl;
    
    for (int i{0}; i<NumberOfMoves; i++){
        // perform evolution
        Grid G_test (BackwardReptation(G, 0)); 

        // check if G_test is a good structure
        if (G_test is good){ 
        // assign
        G = G_test; 
        }
    }

When I do this, I get the error object of type 'Grid' cannot be assigned because its copy assignment operator is implicitly deleted. copy assignment operator of 'Grid' is implicitly deleted because field 'x' is of const-qualified type 'const int'.

I need to evolve Grid multiple times, check if it is good at each evolution step, and change its contents. How can I do this given that I want the geometry of Grid and the energy surface (Emm, Ess, Ems_n, Ems_a) are to be held const?

edit: The following addition to my classobject worked:

    Grid& operator=(const Grid& t){
        return *this; 
    } 

I understand that & is an overloaded operator, can be used to refer to addresses of variables, can be used for rvalues, lvalues. My question is, how does one read the syntax Grid& operator=? What does Grid& mean?

bad_chemist
  • 283
  • 1
  • 7
  • 1
    The error is pretty clear. You can't change const values. – 273K Dec 19 '21 at 17:38
  • @S.M. i have made small edit. i apologize if this is a basic question. – bad_chemist Dec 19 '21 at 17:43
  • 1
    You might need a introductory textbook about C++ if you do not know what reference is. C++ can't be learned by trial and error and guessing ... it is bit too complex. – Öö Tiib Dec 19 '21 at 17:47
  • You won't be able to copy objects of type `Grid` unless you write a copy constructor and a copy assignment for your `Grid` class. They will be deleted because you are defining a destructor. Check the [rule of three](https://en.cppreference.com/w/cpp/language/rule_of_three). – rturrado Dec 19 '21 at 17:47
  • *"How can I do this given that I want the geometry of Grid and the energy surface (Emm, Ess, Ems_n, Ems_a) are to be held const?"* -- what exactly is the "this" that you want to do? (All we know is that it is the result of some function called `BackwardReptation`.) Can you assure us that the "this" that you want to do should change only the non-const members of `Grid`? (If so, it seems like your `Grid` class encompasses two concepts, so potentially should be split into two classes.) – JaMiT Dec 19 '21 at 18:30
  • *"I understand that & is an overloaded operator, can be used to refer to addresses of variables, can be used for rvalues, lvalues. My question is, how does one read the syntax Grid& operator=? What does Grid& mean?"* -- As you wrote, `&` can be used for lvalues. Why the confusion? – JaMiT Dec 19 '21 at 18:34
  • @JaMiT I want to take all the components of G_test and give them to G. I create G_test because I am not sure the update I perform on G is a good one or not. – bad_chemist Dec 19 '21 at 18:34
  • @bad_chemist If "give them to G" involves changing the geometry or energy surface of `G`, then your goal is at odds with your desire to keep those constant. – JaMiT Dec 19 '21 at 18:36
  • It does not involve changing the geometry of ```G```. Only the other components, like ```PolymersInGrid, SolventInGrid, OccupancyMap and Energy```. – bad_chemist Dec 19 '21 at 18:40
  • @bad_chemist Then write a copy assignment operator that tells your compiler that only those fields will change. – JaMiT Dec 19 '21 at 18:57
  • Does this answer your question? [In C++, can a class with a const data member not have a copy assignment operator?](https://stackoverflow.com/questions/60535091/in-c-can-a-class-with-a-const-data-member-not-have-a-copy-assignment-operator) – JaMiT Dec 19 '21 at 19:03
  • I assume the `const` members are invariants, right? As in: they would be the same across all the instances of `Grid`? If so you can make them static, then default the copy-assignment operator with `Grid& operator =(Grid const&) = default;` otherwise you'll need to leave those members mutable. – viraltaco_ Dec 19 '21 at 20:48

0 Answers0