本文介绍了TOUGH:处理C ++中深层嵌套的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了这个结构:

struct s_molecule
{
  std::string res_name;
  std::vector<t_particle> my_particles;
  std::vector<t_bond> my_bonds;
  std::vector<t_angle> my_angles;
  std::vector<t_dihedral> my_dihedrals;

  s_molecule& operator=(const s_molecule &to_assign)
  {
    res_name = to_assign.res_name;
    my_particles = to_assign.my_particles;
    my_bonds = to_assign.my_bonds;
    my_angles = to_assign.my_angles;
    my_dihedrals = to_assign.my_dihedrals;
    return *this;
  }
};

和这些结构:

typedef struct s_particle
{
  t_coordinates position;
  double charge;
  double mass;
  std::string name;
  std::vector<t_lj_param>::iterator my_particle_kind_iter;

  s_particle& operator=(const s_particle &to_assign)
  {
    position = to_assign.position;
    charge = to_assign.charge;
    mass = to_assign.mass;
    name = to_assign.name;
    my_particle_kind_iter = to_assign.my_particle_kind_iter;
    return *this;
  }
} t_particle;

struct s_bond
{
  t_particle * particle_1;
  t_particle * particle_2;
  std::vector<t_bond_param>::iterator my_bond_kind_iter;

  s_bond& operator=(const s_bond &to_assign)
  {
    particle_1 = to_assign.particle_1;
    particle_2 = to_assign.particle_2;
    my_bond_kind_iter = to_assign.my_bond_kind_iter;
    return *this;
  }
};

然后在我的代码中,我返回一个指向s_molecule的指针(typedef'd到t_molecule, )。

and then in my code I return a pointer to an s_molecule (typedef'd to t_molecule, but still).

使用这个指针我可以得到这个代码工作:

Using this pointer I can get this code to work:

for  (unsigned int i = 0;
      i < current_molecule->my_particles.size();
      i++)
    {
      std::cout << "Particle "
        << current_molecule->my_particles[i].name << std::endl
            << "Charge: "
        << current_molecule->my_particles[i].charge << std::endl
        << "Mass: "
        << current_molecule->my_particles[i].mass << std::endl
        << "Particle Kind Name: "
        << (*current_molecule->my_particles[i].my_particle_kind_iter).atom_kind_name
        << std::endl
        << "x: " << current_molecule->my_particles[i].position.x
        << " y: " << current_molecule->my_particles[i].position.y
    #ifdef USE_3D_GEOM
        << "z: " << current_molecule->my_particles[i].position.z
    #endif
        << std::endl;
    }

如果我将其替换为:

for  (std::vector<t_particle>::iterator it = current_molecule->my_particles.begin();
      it !=current_molecule->my_particles.end();
      it++)
    {
      std::cout << "Particle "
        << (*it).name << std::endl
            << "Charge: "
        << (*it).charge << std::endl
        << "Mass: "
        << (*it).mass << std::endl
        << "Particle Kind Name: "
        << (*(*it).my_particle_kind_iter).atom_kind_name
        << std::endl
        << "x: " << (*it).position.x
        << " y: " << (*it).position.y
    #ifdef USE_3D_GEOM
        << "z: " << (*it).position.z
    #endif
        << std::endl;
    }

我现在得到了恶劣的segfaults ...

I now get nasty segfaults...

不要在这里放太多,但是当我尝试这么做时,我也得到了segfaults:

Not to put too much here, but I'm also getting segfaults when I tried to do this:

std::cout << "Bond ATOMS : "
          << (*current_molecule).my_bonds[0].particle_1->name
          << std::endl

同样,current_molecule是一个指向s_molecule结构的指针,这反过来又直接有vars或是指针。我不能得到这些多层间接工作。修正这些段错误的建议。

Again, current_molecule is a pointer to a s_molecule structure, which contains arrays of structures, which in turn either directly have vars or are pointers. I can't get these multiple layers of indirection to work. Suggestions on fixing these segfaults.

FYI我在Linux Centos 5.4上使用g ++编译并使用自定义makefile系统。

FYI I'm compiling on Linux Centos 5.4 with g++ and using a custom makefile system.

推荐答案

同样,这个问题在这里回答:

由DeadMG。对不起,这是双重职位。

Again, this issue was answered here:http://stackoverflow.com/questions/3136586/weird-pointer-issue-in-cby DeadMG. Sorry for the double post.

这篇关于TOUGH:处理C ++中深层嵌套的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:34