c++ - I keep getting stack overflow error and have tried fixing this for 3 days. I am creating a media library for CDs -


//long story short, trying media library, @ 100% complete loss on why cannot data work. main.cpp

#include "cdclass.h"  bool fills = true;//public switch turn on/off autofill of "data" classes. bool runner = true;//public switch helps program functionality(do not edit)  void main() { int decision; unsigned int total = 5; vector<string> titles; vector<double> time; string artist; string name;  titles.resize(total); time.resize(total);  vector<cdstorage> data;//a vector of classes  cdstorage newcd; data.push_back(newcd); data.push_back(newcd); data.push_back(newcd);//this used sizing test , works. cdstorage::cdstorage(total);     //i used loop without restarting main.  while(runner == true) {     if(fills == true)//autofill program running     {         artist = "bunny";         name = "bread";          for(unsigned int x = 0; x < data.size(); x++)         {             cdstorage::cdstorage(total);             data[x].setnewartist(artist);             data[x].setnewname(name);              for(unsigned int y = 0; y < total; y++)             {                 titles[y] = "testfieldbanana!";                 time[y] = 12.13;                 data[x].setnewtitles(y, titles[y]);                 data[x].setnewtime(y, time[y]);             }         }         fills = false;     }      cout << data[0].getnewartist() << endl;     cout << "*******************" << endl <<             "*media awesomsauce*" << endl <<             "*******************" << "\n\n" <<             "********************" << endl <<             "* 1: check library *" << endl <<             "* 2: add cd        *" << endl <<             "* 3: delete cd     *" << endl <<             "* 4: exit program  *" << endl <<             "********************" << "\n\n" <<             "decision:_";      cin >> decision;  //the majority of of self explanatory.      if(decision == 1)     {         for(unsigned int x = 0; x < data.size(); x++)         {             cdstorage::cdstorage(total);                 cout << data[x].getnewname() << "\t";                 cout << data[x].getnewartist() << "\t";             for(unsigned int y = 0; y < total; y++)             {                    //int length = data[x].getnewname().length();                  cout << "\t\t\t" << data[x].getnewtitles(y);                 cout << "\t" << data[x].getnewtime(y) << endl;             }         }      }else if(decision == 2)     {         data.push_back(newcd);          system("cls");          cout << "what name of cd: ";         cin >> name;         cout << "\nwhat name of artist: ";         cin >> artist;         cout << "\nhow many songs there: ";         cin >> total;          cdstorage::cdstorage(total);         titles.resize(total);         time.resize(total);         data[data.size()].setnewname(name);         data[data.size()].setnewartist(artist);          cout << "what song titles , lengths:\n";          for(unsigned int x = 0; x < total; x++)         {             cout << "title " << x+1 << ": ";             getline (cin, titles[x]);             cout << "length(example: 3.36 3 mins , 36 seconds): ";             cin >> time[x];             cout << endl;              data[data.size()].setnewtitles(x, titles[x]);             data[data.size()].setnewtime(x, time[x]);         }      }else if(decision == 3)     {      }else if(decision == 4)     {         runner = false;      }else     {         system("cls");         cout << "error: must choose number between 1-5...\n\n";         system("pause");         system("cls");     }   } } 

//this cdworks.cpp

#include "cdclass.h"  //constructor cdstorage::cdstorage(){}; //overloaded constructor cdstorage::cdstorage(unsigned int thetotal) { newtotal = thetotal; newtitles.resize(newtotal); newtime.resize(newtotal); } //accessors unsigned int cdstorage::getnewtotal() const {     return newtotal; }  string cdstorage::getnewtitles(unsigned int x) const {     return newtitles[x]; }  double cdstorage::getnewtime(unsigned int x) const {     return newtime[x]; }  string cdstorage::getnewartist() const {     return newartist; }  string cdstorage::getnewname() const {     return newname; }  //mutators void cdstorage::setnewtotal(unsigned int thetotal) {     newtotal = thetotal; }  void cdstorage::setnewtitles(unsigned int x, string thetitle) {     newtitles[x] = thetitle; }  void cdstorage::setnewtime(unsigned int x, double thetime) {     newtime[x] = thetime; }  void cdstorage::setnewartist(string theartist) {     newartist = theartist; }  void cdstorage::setnewname(string thename) {     newname = thename; } //destructor cdstorage::~cdstorage(){} 

//this cdclass.h

#include <iostream> #include <string> #include <vector>  using namespace std;  #ifndef cdclass_h #define cdclass_h  class cdstorage { private: unsigned int newtotal; vector<string> newtitles; vector<double> newtime; string newartist; string newname;  public: //constructor cdstorage(); //overloaded constructor cdstorage(unsigned int); //destructor ~cdstorage(); //accessors unsigned int getnewtotal() const; string getnewtitles(unsigned int) const;//the integer track element needs returned. double getnewtime(unsigned int) const; string getnewartist() const; string getnewname() const; //mutators void setnewtotal(unsigned int); void setnewtitles(unsigned int, string); void setnewtime(unsigned int, double); void setnewartist(string); void setnewname(string); };  #endif 

data[data.size()] accessing outside vector data, undefined behaviour, can happen.

also, don't know think repeatedly calling cdstorage::cdstorage(total); does, doesn't except create new (anonymous) object thrown away.

all cdstorages have created created using default (parameterless) constructor, leaves newtotal totally uninitialized, , vectors both empty. can't modify them calling constructor afterwards (i suspect yo're trying accomplish).

since vectors empty, when e.g. newtitles[x] = thetitle;, you're accessing invalid memory, means program, again, has undefined behaviour.

it's difficult whether these cause of problems, should fix them first before go on.

you should review chapter on constructors , instance creation in fine book.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -