vector of pointers to objects

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
// Body.h
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#ifndef BodyH
#define BodyH
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

#include <vector>

class Body
{
public:
Body(std::vector<double> vec); // using a vector to initialize a body

double GetM() const {return M_;}
std::vector<double> GetPosition() const {return Position_;}
std::vector<double> GetVolecity() const {return Volecity_;}

private:
double M_;
std::vector<double> Position_;
std::vector<double> Volecity_;

};

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#endif
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// end of file
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Body.cpp
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

#include "Body.h"

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// functionality
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Body::Body(std::vector<double> vec)
{
M_ = vec[0];
Position_.assign(vec.begin() + 1, vec.begin() + 3);
Volecity_.assign(vec.begin() + 4, vec.begin() + 6);;

}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// end of file
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Input.h
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#ifndef InputH
#define InputH
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

#include <vector>

class Output;
class Body;

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// class Input
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

class Input
{
public:
Input();
~Input();

std::vector<Body*> GetScenario() const {return Scenario_;}
std::vector<std::vector<double>> InitialPosition() const;
std::vector<std::vector<double>> InitialVolecity() const;
long GetN() const {return N_;}
long GetM() const {return M_;}
char GetMethodType() const {return m_type_;}

long GetOutputInterval() const {return OutputInterval_;}
Output * GetOutput() const {return out_;}

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

Body* S_;
Body* E_;
//Body* V_;
//Body* Ma_;
//Body* J_;
//Body* Ne_;

long OutputInterval_;
Output * out_;
};

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#endif
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// end of file
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Input.cpp
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

#include "Input.h"
#include "Body.h"

#include <vector>
#include <cmath>

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// functionality
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Input::Input()
{
std::vector<double> S = {1.989 * std::pow(10,30), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
std::vector<double> E = {5.972 * std::pow(10,24), 1.496 * std::pow(10,11), 0.0, 0.0, 0.0, 2.978 * std::pow(10,4), 0.0};
//std::vector<double> V = {};
//std::vector<double> Ma = {};
//std::vector<double> J = {};
//std::vector<double> Ne = {};

S_ = new Body(S);
E_ = new Body(E);
Scenario_ = {S_, E_};


T_ = 10000;
M_ = T_ * 10000;
N_ = 2;
m_type_ = 'E';

OutputInterval_ = 50000;
}

Input::~Input()
{
delete S_;
delete E_;
//delete V_;
//delete Ma_;
//delete J_;
//delete Ne_;
}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// InitialPosition
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

std::vector<std::vector<double>> Input::InitialPosition() const
{
std::vector<std::vector<double>> InitialPosition;
for(long i = 0; i != N_; ++i)
{
InitialPosition[i] = Scenario_[i] -> GetPosition();
}
return InitialPosition;
}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// InitialVolecity
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

std::vector<std::vector<double>> Input::InitialVolecity() const
{
std::vector<std::vector<double>> InitialVolecity;
for(long i = 0; i != N_; ++i)
{
InitialVolecity[i] = Scenario_[i] -> GetVolecity();
}
return InitialVolecity;
}


//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// end of file
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
How can we run it without a main?
And if there's some particular input I need to give it to make it crash, what is it?
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.
1
2
3
4
5
6
Body::Body(std::vector<double> vec)
{
    M_ = vec[0];
    Position_.assign(vec.begin() + 1, vec.begin() + 4);
    Volecity_.assign(vec.begin() + 4, vec.begin() + 7);;
}


You seem to be unaware of scientific notation:
1
2
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.

And you've misspelled velocity. :)
Thank you very much! I am still learning and thank you for your reply!
Topic archived. No new replies allowed.