Struct e Puntatori C++

di il
1 risposte

Struct e Puntatori C++

Buongiorno a tutti!
Ho provato a scrivere questo programma che mi ha assegnato il mio prof, ma in fase di esecuzione mi da un errore. Vi inoltro testo e codice:
"Create a "Particle" struct, with members corresponding to the data
for each particle (charge, 3 momentum components).
Create an "Event" struct, with members corresponding to the data
for each event (event identifier, 3 decay point coordinates,
number of particles, array of pointers to Particle structs, i.e. Particle**).
The "Particle" struct can be nested inside the "Event" struct.

***
Use a dynamic array for the list of pointers to Particle; create
all the Particle structs dynamically, too.

***
Create a function "read" taking as argument an input file stream,
reading an event and returning a pointer to the event or zero 
at the file end. Create the event dynamically.

***
Create a function "dump" taking as argument a reference to const-event
and printing a dump on the screen.
Write on the screen only the numbers, do not add any text as "Event number:"
or "these are the particles:"; the output should be usable as input for
a following run of the program.

***
Create a function "clear" deleting the event and its content, including
all the particles: first of all delete all the Particle structs, then
delete the array of the pointers and at last the Event struct.

***
Create a "main" function taking the file name from the command string, 
reading the file and calling the dump and clear functions."
Io ho spezzato il programma in distinti file:

//MAIN FUNCTION
#include <iostream>
#include <fstream>

#include "read.cxx"
#include "dump.cxx"
#include "clear.cxx"
#include "strutture.cxx"


int main( int argc, char* argv[] ){
	using namespace std;
	const char* nome = argv[1];
	
	Event *Ev;
		
	ifstream data( nome );
	
	while(  !data.eof() )
	{	
		Ev = read( data );
		dump( Ev );
		clear( Ev );	
	}
	
	return 0;
}

//STRUCTS
#ifndef STRUTTURE_CXX_
#define STRUTTURE_CXX_


struct Particle{
	
	int charge;
	float px;
	float py;
	float pz;
	
};

struct Event{
	
	int identifier;
	float x;
	float y;
	float z;
	int numpart;
	
	Particle **particelle;
	
};

#endif

//READ FUNCTION
#ifndef READ_CXX_
#define READ_CXX_

#include <iostream>
#include <fstream>
#include "strutture.cxx"


Event *read( ifstream& data ){
	
	
	Event *toread = new Event;
		
	data >> ( *toread ).identifier >> ( *toread ).x >> ( *toread ).y >> ( *toread ).z >> ( *toread ).numpart;
	
	for( int i = 0; i < ( *toread ).numpart; i++ )
	{
		Particle *part = new Particle;
		data >> ( *part ).charge >> ( *part ).px >> ( *part ).py >> ( *part ).pz;
		
		toread->particelle[i] = part;
	}
	
	return toread;	
}

#endif

//DUMP FUNCTION
#ifndef DUMP_CXX_
#define DUMP_CXX_

#include <iostream>
#include "strutture.cxx"

void dump( const Event*& evento){
	
	using std::cout;
	using std::endl;
	
	cout << evento->identifier << endl << evento->x << endl << evento->y << endl << evento->z << endl << evento->numpart << endl;	
	
	for( int i = 0; i < evento->numpart; i++ )
		cout << evento->particelle[i]->charge << endl << evento->particelle[i]->px << endl << evento->particelle[i]->py << endl << evento->particelle[i]->pz << endl;

	return;
}

#endif

//CLEAR FUNCTION
#ifndef CLEAR_CXX_
#define CLEAR_CXX_

#include "read.cxx"
#include "dump.cxx"
#include "strutture.cxx"

void clear( Event*& evento ){
	
	for( int i = 0; i < evento->numpart; i++ )
		delete evento->particelle[i];
	
	delete[] evento->particelle;
	delete evento;
	
	return;
}

#endif

Ora il problema è mi fa compilare, ma quando eseguo main.cxx mi compare questo errore:

main.exe 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
[main] main 1000 (0) handle_exceptions: Dumping stack trace to main.exe.core

Che errore è? Dove ho sbagliato nel programma?

Grazie in anticipo,
mandi a tutti!

1 Risposte

  • Re: Struct e Puntatori C++

    Ho provato per sfizio a mettere tutto in un unico sorgente e la riga
    void dump( const Event*& evento){
    impedisce la compilazione.
    Prova a sostituire con
    void dump( const Event* const & evento){
Devi accedere o registrarti per scrivere nel forum
1 risposte