在 main() 中访问类成员时C++分段错误



基本上,我正在创建一个由节点和弹簧组成的网格,并且在尝试访问main()Mesh类中定义的nodes向量的元素时,我不断收到分段错误(核心转储(错误。

当我在Mesh类的构造函数中运行测试输出时,我可以很好地访问节点成员。我确定这是一个内存问题,但谁能解释为什么会发生这种情况?

节点类:

class Node
{ 
public:
/// (Non-)magic number indicating that the coordinate has not
/// been classified as pinned or free yet
static int Not_classified_yet;
/// (Non-)magic number indicating that the coordinate is pinned
static int Is_pinned;
/// Constructor: Pass the spatial dimension
Node(const unsigned& dim)
{
// Resize
X.resize(dim,0.0);
Eqn_number.resize(dim,Not_classified_yet);
}

/// Function to add a spring to the node
void add_spring_pt(Spring* spring_pt)
{
Spring_pt.push_back(spring_pt);
}
/// How many springs are attached to this node?
unsigned nspring()
{
return Spring_pt.size();
}
/// Access function to the ith spring connected to the node
Spring*& spring_pt(const unsigned& i)
{
return Spring_pt[i];
}
/// Access function to the position vector of the node
vector<double>& get_vector()
{
return X;
}
/// Access function to the coordinates of the node
double& x(int i)
{
return X[i];
}
/// Access function to the equation number for each coordinate 
/// Can be negative if node is pinned in that direction.
int& eqn_number(const unsigned& i)
{
return Eqn_number[i];
}

/// Pin the i-th coordinate
void pin(const unsigned& i)
{
Eqn_number[i]=Is_pinned;
}
/// Is the i-th coordinate pinned?
bool is_pinned(const unsigned& i)
{
return (Eqn_number[i]==Is_pinned);
}

private:
/// Pointers to the springs attatched to the node. 
vector<Spring*> Spring_pt;
/// Coordinates of the node
vector<double> X;
/// Vector containing equation indices for each coordinate direction.
/// Can be negative if node is pinned in that direction.
vector<int> Eqn_number;
};

网格类:

class Mesh
{
public:
/// constructor (nX contains number of nodes in each direction)
Mesh(const vector<unsigned> nX)
{ 
/// Function "num_nodes" defined in "myFunctions.cpp" to find the 
/// total number of nodes.
unsigned nNodes = num_nodes(nX);
/// Check the dimension of the mesh and and construct a vector
/// of the nodes.
unsigned dim = nX.size();
vector<Node> nodes(nNodes,dim);
//std::cout<< nodes[1].x(0)<<std::endl;
/// Function "num_springs" defined in "myFunctions.cpp" to find the
/// total number of springs.
unsigned nsprings = num_springs(nX);
/// Vector to hold the springs.
vector<Spring> springs(nsprings);
/// Function to assign coordinates to all the nodes.
assign_coordinates(nodes,nX);
}
/// Access function to the ith node of the mesh.
Node& node(const unsigned& i)
{
return nodes[i];
}
/// Function declaration for assigning coordinates to nodes
void assign_coordinates(std::vector<Node>& nodes, std::vector<unsigned> nX);
/// Access function to the ith spring of the mesh.
Spring& spring(const unsigned& i)
{
return springs[i];
}
private:
/// Declare vectors to hold the nodes and springs.
vector<Node> nodes;
vector<Spring> springs;
};

以及我试图从main((输出的内容:

int main()
{
// create a mesh
// spatial dimensions
unsigned nx = 3;
unsigned ny = 3;
unsigned nz = 3;
vector<unsigned> nX(2);
nX[0] = nx;
nX[1] = ny;
//nX[2] = nz;
Mesh m(nX);
// segmentation fault (core dumped)
std::cout << m.node(6).eqn_number(1) << std::endl;
};

提前感谢任何帮助。

当你定义时,问题(问题?(Mesh构造函数中

vector<Node> nodes(nNodes,dim);

我想你的意图是初始化Mesh类的nodes成员;你得到的是一个Mesh构造函数局部的nodes变量。

Meshnodes成员是用隐式默认构造函数初始化的,所以Nodes(Spring_ptXEqn_number(中的向量用默认的构造函数初始化std::vector因此大小为零。

当您致电时

m.node(6).eqn_number(1)

使用node(6)调用大小为 0 的std::vector的第 7 个元素。

同样的问题

std::vector<Spring> springs(nsprings);  

Mesh构造函数中:您在构造函数中声明并初始化一个本地springs变量,您的意图(我想(是初始化Meshsprings成员。

如果我正确理解您的意图,您应该能够解决编写构造函数的问题,如下所示

Mesh (const std::vector<unsigned> nX)
: nodes(num_nodes(nX), nX.size()), springs(num_springs(nX))
{ assign_coordinates(nodes,nX); }

乍一看,您似乎用 2 个节点初始化了网格,然后请求第 7 个节点(索引从 0 开始(。

检查这一点的一种快速方法是将nodes[i]更改为nodes.at(i)这将启用矢量元素访问的越界检查。

最新更新