I am trying to create a std::vector (DIIS_Tensors) that holds a few (DIIS_num_iters) iterates of a tensor I'm converging.
I initialized DIIS_Tensors as such:
std::vector<TensorRank4> DIIS_Tensors(DIIS_num_iters);
where TensorRank4 is a custom class defined below:
class TensorRank4 {
public:
TensorRank4(size_t dim0, size_t dim1, size_t dim2, size_t dim3) {
dims_[0] = dim0;
dims_[1] = dim1;
dims_[2] = dim2;
dims_[3] = dim3;
data_.resize(dims_[0] * dims_[1] * dims_[2] * dims_[3]);
}
double& operator ()(size_t i, size_t j, size_t k, size_t l) {
return data_(index(i, j, k, l));
}
const double& operator ()(size_t i, size_t j, size_t k, size_t l) const {
return data_(index(i, j, k, l));
}
// std::vector<double> resizeR4TensortoVector(const TensorRank4 &t_dim4, int dim0, int dim1, int dim2, int dim3){
void setZero() { data_.setZero();}
void clear() {
data_.resize(0);
std::fill(dims_, dims_ + 4, 0); // It may not be std::fill but you get the posize_t.
}
private:
size_t index(size_t i, size_t j, size_t k, size_t l) const {
return i * dims_[3] * dims_[2] * dims_[1]
+ j * dims_[3] * dims_[2]
+ k * dims_[3]
+ l;
}
size_t dims_[4];
Eigen::VectorXd data_;
};
However, when I run this code, I get the error:
no matching constructor for initialization of 'TensorRank4'
Based on some searching, it seems to be related to not having a default constructor for this class, but I'm confused as to how to work around this issue.
std::vector<TensorRank4> DIIS_Tensors(DIIS_num_iters);
> is trying constructDIIS_num_iters
default-inserted elements of typeTensorRank4
.But
TensorRank4
doesn't have default constructor and can't be default-inserted.You can add a default constructor for
TensorRank4
, or pass an instance to the constructor ofvector
.e.g.