c++ - Trouble outputting vector to .txt file -


i've been struggling time , i'm reaching out help. i'm reading in data text file via readdata function. within function i'm calling function of element class read in element data vector of vectors. i'm then trying write data text file via writedata function, , problem is. when compile , run, outputfile contains other data i'm writing except element data. need able access element data via other functions.

any help, tips, or insight appreciated. i've included believe pertinent code below. if more necessary, let me know.

thanks!

readdata function:

void readdata::readdata(ifstream &inp) {      element r;      r.addelementdata(inp);  }; 

addelementdata function:

void element::addelementdata(istream& inp)  {     int i,j,tmp;      (i = 0; < d_numelems; i++)     {         vector<int>row;         (j = 0; j < 10; j++)         {             inp >> tmp;             row.push_back(tmp);          }     elems_matrix.push_back(row);     }  } 

output matrix contents function:

void element::elems_matrix_contents(ofstream& out) {     int i, j;      (i = 0; < d_numelems; i++)     {         (j = 0; j < 10; j++)         {             out << elems_matrix[i][j] << '\t';         }     } } 

element class:

class element: public readdata { protected:     std::vector< std::vector<int> > elems_matrix;  public:     // constructors     element();      //destructor     ~element(){};     // functions     void addelementdata(istream&inp);      std::vector<std::vector < int >> elems_matrix_copy;      void elems_matrix_contents(ofstream& out);  }; 

readdata class:

class readdata  { public:      readdata();      ~readdata(){};      void readdata(ifstream &inp);     void writedata(ofstream &out);  }; 

writedata function:

void readdata::writedata(ofstream &out) {      out << "element  " << "material  " << "nodes:  "<< endl;      element r;      r.elems_matrix_contents(out);      out << "*******************************************************************" << endl << endl; }; 

the problem create local variable of type element , read data that. because local destroyed @ end of function:

void readdata::readdata(ifstream &inp) {      element r; // die @ end of function      r.addelementdata(inp);  }; 

same writedata() function. create brand new (empty) element , output that:

void readdata::writedata(ofstream &out) {      out << "element  " << "material  " << "nodes:  "<< endl;      element r; // new contains nothing      r.elems_matrix_contents(out);      out << "********************************************" << endl << endl; }; 

perhaps should make element r member of class readdata?

class readdata {     element r; // functions can use same element      // ... blah blah blah }; 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -