C++自定义模板类的向量和不干净(?)分配
我有一个非常复杂的问题,找不到问题在哪里。问题的一部分可能是,我没有完全理解数据结构。我有以下自定义模板类,带有此成员变量:
template <class T>
class Matrix
{
private:
T * *M; /* array of row pointers */
size_t n_rows, /* number of rows */
n_cols; /* number of columns */
size_t row_start, /* first row of submatrix */
col_start, /* first column of submatrix */
origcols; /* refers to original matrix */
bool hasownvalues, /* has matrix own value? */
initialized; /* are matrix values initialed? */
我还制作了一个自定义构造函数,该构造函数从文件中制成了一个带有选项卡的数据的文件:
Matrix(const std::string &filename)
{ do.stuff }
我不确定,这真的很重要,它很大程度上是代码。如果是这样,我可以稍后进行编辑,但是只要我声明为该对象声明一个变量,构造函数就可以运行良好。 当我尝试制作一堆对象并尝试将它们包裹在向量中时,就会发生我的问题。可以说我有X不同的文件,每个文件都包含矩阵的数据。我还拥有一个存储文件名的向量:
std::vector<std::string> filenames{};
for (int idx = 3; idx < argc; idx++)
{
filenames.push_back(argv[idx]);
}
到目前为止,该代码可行,并且我必须保持不变以保持其余程序的工作。 以下是灵活的,这就是我有问题的地方:
std::vector<Matrix<T>> coredata; /* initialize the vector for the wrap */
...
for (auto& it : filenames)
{
Matrix<T> M(it); /* call the special constructor*/
coredata.push_back(M); /* add new object to the vector */
}
第一个任务效果很好,但是从第二个任务开始,它似乎是附加了新矩阵,但也覆盖了旧的矩阵或至少一部分。在第三个程序中,我在运行程序时会遇到细分故障。
该程序周围的一些信息:
- 构造函数与“新”命令分配的空间合作,也许这可能是问题的一部分。
- 在运行时间之前,矩阵对象的实际尺寸不知道。 (数组的大小可能不同),但是我可以安排,实际上读取数据始终是相同的大小
- t在向量内是恒定的。 无法发生,M有时是矩阵int,有时矩阵浮动
- 该矢量将嵌入另一个超级类中,而上面的剪辑是该类别的构造函数的一部分,Coredata是成员变量。
我很困惑问题到底是什么。也许有些变量寿命不够长。我还考虑过仅分配对向量中的地址的参考,但据我了解,向量已经存储了一个参考,因此似乎并不合理。 我还想到了用一堆“ 0”杂志初始化向量的,然后用构造的对象覆盖它们。但是我不知道该怎么做,最重要的是 我不知道在编译时,读取矩阵将具有多少行和列。 也许矢量根本不是一个有用的解决方案,我还考虑过制作普通的C阵列。 但是我又有问题了,我必须先初始化此数组,而不能仅附加另一个矩阵对象。
I have a quite complex Problem and couldn't find out, where the Problem is. Part of the Problem might be, that i didn't fully understand the data structures. I have the following custom template class, with this member variables:
template <class T>
class Matrix
{
private:
T * *M; /* array of row pointers */
size_t n_rows, /* number of rows */
n_cols; /* number of columns */
size_t row_start, /* first row of submatrix */
col_start, /* first column of submatrix */
origcols; /* refers to original matrix */
bool hasownvalues, /* has matrix own value? */
initialized; /* are matrix values initialed? */
I also made a custom constructor, which makes a 2D array out of a file with tab-separated data:
Matrix(const std::string &filename)
{ do.stuff }
I'm not sure, if it is really important how this exactly works, it quite a bunch of code. if so, i could edit it later, but the constructor works well as long as i declare a single variable for such an object.
My problem occurs when i try to make a bunch of the objects and try to wrap them up in a vector. Lets say i have x different files, each containing data for a matrix. I also have a vector in which the filenames are stored:
std::vector<std::string> filenames{};
for (int idx = 3; idx < argc; idx++)
{
filenames.push_back(argv[idx]);
}
So far this code works and it is mandatory that i keep it unchanged to keep the rest of the program working.
The following is flexible and thats where i have problem:
std::vector<Matrix<T>> coredata; /* initialize the vector for the wrap */
...
for (auto& it : filenames)
{
Matrix<T> M(it); /* call the special constructor*/
coredata.push_back(M); /* add new object to the vector */
}
The first assignment works well, but from the second on it seems that it appends the new Matrix, but also overwrites the old one or at least a part of it. And in the third one i get a segmentation fault when running the program.
Some information of the program around:
- the constructor works with space allocated by the "new" command, maybe that could be part of the problem.
- the real size of the Matrix object isn't known before run time. (the arrays can be of different size), but i could arrange, that the read data in fact always is of the same size
- T is constant within the vector. it can't happen, that M is sometimes Matrix int and sometimes Matrix float
- the vector would be embedded in another superclass and the snippet above is part of a constructor of this class, coredata is a member variable.
I'm quite puzzled what the problem exactly is. Maybe some variables don't live long enough. I also thought of assigning just a reference to the address in the vector, but as far as i understand, a vector already stores just a reference, so it seems not that plausible.
I also thought of initializing the vector with a bunch of "0"-matrices and then overwrite them with the constructed objects. But i don't know how to do that and on top of that,
i don't know at compile time how many rows and columns the read Matrices will have.
Maybe a vector isn't a useful solution at all, i also thought of making a plain C-array.
But there i have the problem again, that i have to initialize this array first and can't just append another Matrix-Object to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论