提问人:bad_chemist 提问时间:12/20/2021 最后编辑:bad_chemist 更新时间:12/20/2021 访问量:60
如何将对象的内容复制到已经定义的对象中?
How to copy contents of an Object into an already defined object?
问:
我有一个名为的对象,它在我的主脚本中正在演变。我是这样定义的:Grid
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(){
};
...
};
在我的主脚本中,当我将运算符应用于 Grid 时,我的 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;
}
}
当我这样做时,我收到错误.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'
我需要多次进化 Grid,检查它是否擅长每个进化步骤,并更改其内容。鉴于我希望保持网格的几何形状和能量表面 (),我该怎么做?Emm, Ess, Ems_n, Ems_a
const
编辑:对我的类对象的以下添加起作用了:
Grid& operator=(const Grid& t){
return *this;
}
我知道&是一个重载运算符,可以用来指代变量的地址,可以用来表示右值、左值。我的问题是,如何阅读语法?Grid& 是什么意思?Grid& operator=
答: 暂无答案
评论
Grid
Grid
BackwardReptation
Grid
Grid