c++ 通过双指针访问结构时出现问题

lxkprmvk  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(72)

我是c++新手,还在理解指针是如何工作的。基本上,我在头文件中有一个类,在其中定义了一个strct。在一个单独的cpp文件中定义方法,我需要访问该结构的成员以初始化它们。在类中,我定义了一个结构类型的双指针。在类构造函数中,我创建了一个这些指针的数组,并在add方法中创建了指针数组的特定元素的结构。问题就在后面,因为我不能访问结构成员。在前面的练习中,我做了这种逻辑工作,但我只需要使用结构,而不是类,所以我真的不知道。建议?
这是带有类声明的头文件,双指针在最后

#ifndef Event_h
#define Event_h

class Event {

public:

 // constructor
  Event( int n, float x, float y, float z ); // create an event with number "n"                                                  // and coordinates x, y, z
 // destructor
 ~Event();

 // composite object Particle to hold all information for each particle
 // ( electric charge and x,y,z momentum components )
 struct Particle {
   int Charge;
   float P_x, P_y, P_z;
 };
 typedef const Particle* part_ptr;

// add a particle to the event
void add( int charge, float px, float py, float pz ); //APPEND A PARTICLE TO THE EVENT

// get event id.
int eventNumber() const; //int function(parameters) const
                       //function is a method which does not modify any of 
                       //the member variables in the object instance for which it is being invoked.
// get decay point coordinates
float x() const;
float y() const;
float z() const;
// get number of particles
int nParticles() const;
// get particle
part_ptr particle( unsigned int i ) const; //funzione

private:

// event-specific informations:
int evNumber; // event id
int nParticles_; //number of particles of the event
float x_, y_, z_; // decay point

// particles: number and array of pointers
static const int max=10; //perché è necessario static const
part_ptr *ptrParticle;

};

#endif

字符串
以下是方法定义:

#include "Event.h"

// constructor
Event::Event( int n, float x, float y, float z ):
// initializations
evNumber(n), x_(x), y_(y), z_(z) {
// allocate a buffer for particle pointers
ptrParticle = new Particle*[max];
}

// destructor 
Event::~Event() {
// delete all the particle pointers
for (int i = 0; i < max; i++)
//delete[] ptrParticle[i]; //???

// delete the pointers array
delete[] ptrParticle;
}

void Event::add( int charge, float px, float py, float pz ) {

// check for the number of particles, if maximum reached do nothing
// and return
// create the new particle and fill with data
if(nParticles_ < max)
{

  ptrParticle[nParticles_] = new Particle;
  //int size = sizeof(ptrParticle);
  *(ptrParticle[nParticles_])->Charge = charge; //the problems are hereeeeee!
  *(ptrParticle[nParticles_])->P_x = px;

 // store the new particle pointer in the array and increase counter
 //...
 ++nParticles_;  
}
return;
}


它给出的错误是“'*'的操作数必须是指针,但类型为“int””
我在论坛上查找类似的问题,而且,只是为了绝望,解引用操作符的不同用法,我试图使用一个指针,只是一点的一切,但我真的不明白什么是错的。

  • 我看到有人用malloc,教授没有真正涉及到这一点,所以如果有一种方法,不涉及它;
  • 我们还没有使用vector类
jxct1oxe

jxct1oxe1#

*(ptrParticle[nParticles_])->Charge = charge; //the problems are hereeeeee!

字符串
ptrParticle是一个

part_ptr *ptrParticle;


因此,根据C++的规则,ptrParticle[nParticles_]将是part_ptr,这是一个

const Particle*


因此,在表达式中使用->运算符解引用它,访问Charge,这是一个int
1.因为这是一个const指针,所以这将是const int。根据C的规则,你不能给const对象赋值任何东西。
1.此外,一元*不能应用于int,这在C
中也是不允许的。这相当于,例如:

int n;

*n = 4;


很明显,这完全是胡说八道。
退一步说:

ptrParticle[nParticles_] = new Particle;


因为ptrParticle是一个指向***常量*对象的指针数组,一旦你在里面塞了什么东西,你就会自动丧失改变该对象中任何东西的权利,直到我们的宇宙热寂。这就是C++中*常量对象的含义。
要正确执行此操作,必须执行以下事件顺序。
1.使用new创建新的Particle对象,并将其存储在指针中。
1.初始化此对象。使用指针将其Charge设置为您想要设置的任何内容。在该对象中设置您想要设置的任何其他内容。
1.将指向new-艾德对象的指针存储在此数组中 * 在
对象初始化后。

相关问题