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

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

基本上,我正在创建一个由节点和 Spring 组成的网格,并且在尝试访问 Mesh 中定义的 nodes vector 的元素时,我不断收到段错误(核心转储)错误 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;
};

在此先感谢您的帮助。

最佳答案

问题(一个问题?)在你定义的Mesh构造函数中

 vector<Node> nodes(nNodes,dim);

我想您的意图是初始化 Mesh 类的 nodes 成员;您得到的是 nodes 变量,它是 Mesh 构造函数的本地变量。

Meshnodes 成员使用隐式默认构造函数进行初始化,因此 Node 中的 vector (Spring_ptXEqn_number) 使用默认的 std::vector 构造函数进行初始化,因此大小为零。

当你打电话时

m.node(6).eqn_number(1)

node(6) 中,您调用大小为零的 std::vector 的第 7 个元素。

同样的问题

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

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

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

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

关于在 main() 中访问类成员时出现 C++ 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45016830/

相关文章:

c++ - 将 unsigned char* 数组转换为 std::string

java - 运行一个在 windows 和 Linux 平台上运行的 Java 开发的 web 项目

linux - SteamBot.exe 没有出现在 SteamBot/Bin/Release 中

android - 在 VS2015 上使用 -std=c++11

c++ - 动态内存分配/利用未使用的内存

c++ - Visual C++ 2012 在类中使用 std::vector 或 std::list

c++ - Windows 10 上 VS 2015 中 nupengl GLFW 的链接错误

Linux 内核导出符号

c++ - 获取最大的无符号整数类型

c++ - 将指向项目的指针存储在 std::set 中是否安全?