我有当前的课程
class VectorVectorRow : public virtual RowFormat {
private:
std::vector<std::vector<double>>* mat;
int r_pos;
public:
VectorVectorRow (std::vector<std::vector<double>>* r,int pos) : mat{r}, r_pos{pos} {};
VectorVectorRow (const VectorVectorRow& a) : mat(a.mat), r_pos{a.r_pos} {}
//Where the SIGABRT exception is risen
virtual ~VectorVectorRow() = default; // inline;
virtual double getCell(int pos);
virtual int getSize();
virtual void setCell(int pos, double value);
};
继承了以下虚拟类:
class RowFormat { public:
RowFormat() {};
/**
* @param pos returns the cell at the current position
*/
virtual double getCell(int pos) = 0;
/** returns the size of the row
*/
virtual int getSize() = 0;
/**
* @param pos position of the cell
* @param value value that has to be stored
*/
virtual void setCell(int pos, double value) = 0;
void increment(int pos, double value) {
setCell(pos,getCell(pos)+value);
}
virtual ~RowFormat() =default; };
第一个类的实例按以下方式使用:
for(int epoch=0; epoch < training_epochs; epoch++) {
for (int row=0; row < train_N; row++) {
unique_ptr<RowFormat> k{std::move(df->getRow(row))}; //getRow returns an unique_ptr of VectorVectorRow up-casted to RowFormat
da.train(k.get(),learning_rate,corruption_level);
// just before the block close, a SIGABRT exception is risen
}
}
getRow
的特定实例的df
定义如下:
// std::shared_ptr<std::vector<std::vector<double>>> mat;
std::unique_ptr<RowFormat> VectorVectorData::getRow(int pos) {
std::unique_ptr<VectorVectorRow> ptr{new VectorVectorRow(mat.get(),pos)};
std::unique_ptr<RowFormat> casted{dynamic_cast<RowFormat*>(ptr.get())};
return casted;
}
我甚至尝试用std :: shared_ptr替换std::vector<std::vector<double>>* mat;
,但错误仍然存在。我怎么能避免在块结束时的执行?提前谢谢。