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

时间:2017-07-10 16:06:50

标签: c++ linux c++11 initialization sublimetext3

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

当我在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;
};

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

快速浏览一下,看起来你用2个节点初始化你的网格,然后去寻求第7个(索引从0开始)。

检查此问题的一种快捷方法是将nodes.at(i)更改为{{1}},这将启用向量元素访问的越界检查。

答案 1 :(得分:0)

问题(问题?)在Collections.replaceAll(list, foo, bar); 构造函数中定义

Mesh

我想你的意图是初始化 vector<Node> nodes(nNodes,dim); 类的nodes成员;你得到的是一个Mesh变量,它是nodes构造函数的本地变量。

使用隐式默认构造函数初始化Mesh的{​​{1}}成员,因此nodes中的向量(MeshNode和{{ 1}})使用默认的Spring_pt构造函数初始化,因此大小为零。

致电时

X

使用Eqn_number调用大小为零的std::vector的第7个元素。

相同的问题
m.node(6).eqn_number(1)
node(6)构造函数中的

:您在构造函数中声明并初始化std::vector变量,其中您的意图(我猜)是初始化std::vector<Spring> springs(nsprings); Mesh成员。

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

springs