[英]Initialize vector with M elements of empty vectors c++
Setting m_data.resize(a_M) does work but I would like to know why this error occurred.设置 m_data.resize(a_M) 确实有效,但我想知道为什么会发生此错误。
error: type 'vector<vector<double> >' does not provide a call operator
m_data(a_M);
This is the beginning of a class SparseMatrix.这是 class SparseMatrix 的开始。 I need to initialize the row number a_M and have each element be empty.
我需要初始化行号 a_M 并使每个元素为空。 The idea is for m_data(a_M) initialize m_data to have a_M rows of empty vectors, though the error above occurred.
这个想法是 m_data(a_M) 将 m_data 初始化为具有 a_M 行空向量,尽管发生了上述错误。
class SparseMatrix
{
public:
SparseMatrix();
SparseMatrix(int a_M, int a_N);
private:
unsigned int m_m, m_n;
double m_zero;
vector<vector<double> > m_data;
vector<vector<int> > m_colIndex;
};
SparseMatrix::SparseMatrix()
{}
SparseMatrix::SparseMatrix(int a_M, int a_N)
{
m_m = a_M;
m_n = a_N;
m_zero = 0.0;
m_data(a_M);
m_colIndex(a_M);
}
I am still new to C++ so it's these little things that are hard to come by on the internet.我对 C++ 还是新手,所以这些小东西在互联网上很难找到。 I really appreciate the help!
我真的很感激帮助!
First of all,首先,
m_data = ...;
is assignment, not initialization.是赋值,而不是初始化。
Using使用
m_data(a_M);
m_colIndex(a_M);
inside the body of the constructor is not right.构造函数的主体内部是不正确的。 Use
利用
SparseMatrix::SparseMatrix(int a_M, int a_N) : m_m(a_M),
m_n(a_N),
m_zero(0),
m_data(a_M),
m_colIndex(a_M)
{
}
Since the member variables m_m
and m_n
are of type unsigned int
, I would suggest changing the constructr to:由于成员变量
m_m
和m_n
的类型是unsigned int
,我建议将构造函数更改为:
SparseMatrix::SparseMatrix(unsigned int a_M, unsigned int a_N) : m_m(a_M),
m_n(a_N),
m_zero(0),
m_data(a_M),
m_colIndex(a_M)
{
}
std::vector<std::vector<float>> m_data(M);
or或者
std::vector<std::vector<float>> m_data;
m_data.resize(M); //this will resize the vector
for (int i = 0; i < M; i++){
m_data[i].resize(N);//and this will resize the empty vectors.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.