Hi, guys, I am doing a simulation about solar system. The simplest scenario is that containing only sun and earth. I wrote a 'Body' class first to initialize a body using a vector of size 7(mass, position and velocity). Then in 'Input' class I want to specify the scenario using a vector of pointers to body objects. The question is: it compiled but didn't run. There seems to be some 'segmentation fault'. Could you guys give me some advice? Thank you very much!
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#endif
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// end of file
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// end of file
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// class Input
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private:
friend class Output;
void SetOutput(Output & out){out_ = &out;}
std::vector<Body*> Scenario_;
long T_; //year of simulations
long N_; //number of bodies in the system
long M_; //number of simulations
char m_type_; //method type: E for Euler method, R for Runge-Kutta method
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#endif
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// end of file
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// end of file
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Sorry I didn't make it clear. It is a quite big program and I am pretty sure the problem was in Input.cpp so I omitted the other part. It seems like a segmentation fault so I want to know if I made some mistakes when constructing a vector of pointers to objects and I want to know some detail about the memory involved here.
Are you unable to use a debugger? If so, you need to learn. Using a debugger you can determine exactly where it segfaulted and why. But I found an error (and a couple of things you can improve).
You don't use the correct end iterators for the assigns. The end needs to point one-past-the-end.
std::vector<double> S = {1.989e30, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
std::vector<double> E = {5.972e24, 1.496e11, 0.0, 0.0, 0.0, 2.978e4, 0.0};
And you really shouldn't give a local variable name the same name as the function. That's just wacky (although very Pascalish).
Your massive use of vectors is kind of ridiculous for a fixed size 3-element array. If you don't want to use a C-style array, you could use std::array.