Question
HELP WITH C++ PROJECT!! I am trying to get this code to work... cerr < < t=========Test #14: Star Classes =========== ; Starlist *
HELP WITH C++ PROJECT!!
I am trying to get this code to work...
cerr << " \t=========Test #14: Star Classes =========== "; Starlist * sl = new Starlist(); assert(sl->getCurrentNumPlanets() == 0); id_list[0] = sl->addPlanet(); assert(sl->getCurrentNumPlanets() == 1); p = sl->getPlanet(id_list[0]); int pos = p->getPos(); sl->orbit(); assert(p->getPos() == (pos + 1) % 360);
Starvector * sv = new Starvector(); assert(sv->getCurrentNumPlanets() == 0); id_list[1] = sv->addPlanet(); assert(sv->getCurrentNumPlanets() == 1); p = sv->getPlanet(id_list[1]); pos = p->getPos(); sv->orbit(); assert(p->getPos() == (pos + 1) % 360);
//add 200 more planets to each for(int i = 0; i < 200; i+=2){ id_list[i] = sv->addPlanet(); id_list[i+1] = sl->addPlanet(); } //delete 10 random planets in the sun for(int i = 0; i < 50; i++){ int rand_id = (rand() % (200)); if(id_list[rand_id] == -1){ continue; }else if(sv->removePlanet(id_list[rand_id]) || sl->removePlanet(id_list[rand_id])){ id_list[rand_id] = -1; }else{ assert(false); } }
sv->printStarInfo(); sl->printStarInfo();
cerr << " \t\tTest #14 Passed... ";
HERE IS MY STARVECTOR.CPP
#include "Starvector.h" #include
Starvector::~Starvector(){//the destructor is a member function which destructs or deletes an object. // Delete the pointers inside of the array delete vector; }
Starvector::Starvector(){ vector = new Vector(); } bool Starvector::removePlanet(long id){ for(int i = 0; i < (int)vector->size(); i++){ if((vector->read(i))->getID() == id){ vector->remove(i); return true; } } return false; }
long Starvector::addPlanet(){ Planet* temp = new Planet(rand()%3001); vector->insert(vector->size(), temp); return temp->getID(); }
Planet * Starvector::getPlanet(long id){ for(int i = 0; i < (int)vector->size(); i++){ if((vector->read(i))->getID() == id) return vector->read(i); } return NULL;
}
void Starvector::printStarInfo(){ cout<<"The star currently has "<< vector->size() << "Planets."<
} void Starvector::orbit(){ for(int i = 0; i < (int)vector->size(); i++){ (vector->read(i))->orbit(); } } unsigned int Starvector::getCurrentNumPlanets(){ return vector->size(); }
Here is my Starlist.cpp
#include "Starlist.h" #include
Starlist::~Starlist(){ delete data; }
Starlist::Starlist(){ this->data = new List(); } bool Starlist::removePlanet(long id){ for(int i = 0; i < (int)data->size(); i++){ if(data->read(i)->getID() == id){ return data->remove(i); } } return false; }
long Starlist::addPlanet(){ Planet * p = new Planet(rand()%3001); data->insert(data->size(), p); return p->getID(); }
Planet * Starlist::getPlanet(long id){ for(int i = 0; i < (int)data->size(); i++){ if(data->read(i)->getID() == id){ return data->read(i); } } return NULL; }
void Starlist::printStarInfo(){ cout<<"The star currently has "<< data->size() << "planets."<
unsigned int Starlist::getCurrentNumPlanets(){ return this->data->size(); }
void Starlist::orbit(){ for(int i = 0; i < (int)data->size(); i++){ (data->read(i))->orbit(); } }
I am getting this error in the terminal..
=========Test #14: Star Classes ===========
Makefile:12: recipe for target 'run' failed make: *** [run] Segmentation fault (core dumped)
I have been working on this for the past 2 hours and have no idea what i am doing wrong..
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started