c++ - Segfault caused by std::ostream::sentry in this overload of stream operator -


my goal overload << operator objects of param class printed appropriately standard out.

the param class defined in globals.h , globals.cpp files. below these files, there main.cpp test program. further below 3 text input files myparam__lb, myparam__mid, , myparam_ub. put of these files same directory, , it's reduced-size version of real case in question.

here backtrace real case:

#0  0x00007ffff772811e in std::ostream::sentry::sentry(std::ostream&) () /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #1  0x00007ffff7728829 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) () /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #2  0x00007ffff7728c0f in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) () /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #3  0x0000000000450d32 in initsimulation5 () @ ../simulation5.cpp:809 #4  0x000000000042c853 in policy::policy (this=0x7fffffffb090) @ ../globals_mode.cpp:183 #5  0x00000000004325c4 in readpoliciesfromscript (policies=..., filename=...) @ ../globals_utilities.cpp:321 #6  0x0000000000436280 in main (argc=1, argv=0x7fffffffe0d8) @ ../main.cpp:122 

this code correctly running on machine, on current machine, segfaults. (both machines 64-bit ubuntu, different kernels, ubuntu versions, etc.)

question: wrong code overloaded << operator?

globals.h

//globals.h /* initialize param object base file path  * text files containing low, mid, , high estimates.  * if low, mid, , high estimates are, respectively,  * stored in files  *  *    ./input/myparam__lb  *    ./input/myparam__mid  *    ./input/myparam__ub  *  * base file path should "./input/myparam".   */ class param { public:     param(string basefilepath, const int size);      /*! set param low estimates */     void setlevellow();      /*! set param mid estimates */     void setlevelmid();      /*! set param high estimates */     void setlevelhigh();      /*! display range of param values using ostream */     friend ostream& operator<< (ostream &out, param &cparam);      /*      * overloaded operators below allow param object passed      * function (const|nonconst) std::vector<double>& ,      * return reference appropriate set of values based on      * whether param object set low, mid, or high      * estimates.      */      operator std::vector<double>& () {         if( level < 0 ) return lb;         if( level > 0 ) return ub;         return mid;     }      operator const std::vector<double>& () const {         if( level < 0 ) return lb;         if( level > 0 ) return ub;         return mid;     }  private:     int level;      std::vector<double> lb;     std::vector<double> mid;     std::vector<double> ub;      void readratesfromfile(std::vector<double>& x, const string filepath, const int size);  }; 

globals.cpp

void param::setlevellow() { level = -1; }  void param::setlevelmid() { level = 0; }  void param::setlevelhigh() { level = 1; }  ostream& operator<< (ostream &out, param &cparam) {      /* obtain correct level of parameter */     vector<double> *pparam;     switch(cparam.level) {     case -1:         pparam = &(cparam.lb);         out << "-";         break;     case 0:         pparam = &(cparam.mid);         out << "0";         break;     case 1:         pparam = &(cparam.ub);         out << "+";         break;     default:         std::cerr << "level=" << cparam.level << " when calling <<(ostream&,param&)";             exit(1);             break;     }      /* obtain value(s) of parameter */     streamsize pre = cout.precision();     cout.precision(5);     out << " value = ";     double smallest, largest;     smallest = *min_element(pparam->begin(), pparam->end());     largest  = *max_element(pparam->begin(), pparam->end());     if(smallest==largest) {         out << smallest;     } else {         out << "[" << fixed << smallest << ", " <<                       fixed << largest << "]";     }     cout.precision(pre); }  param::param(string basefilepath, const int size) {      /* read lower-bound values file */     readratesfromfile(lb, basefilepath+"__lb", size);      /* read midpoint estimate values file */     readratesfromfile(mid, basefilepath+"__mid", size);      /* read upper-bound values file */     readratesfromfile(ub, basefilepath+"__ub", size);      /* default use midpoint estimate */     setlevelmid(); }  void param::readratesfromfile(std::vector<double>& x, const string filepath, const int size) {      char line[100];      x.clear();     x.resize(size,0.0);      fstream f_in(filepath.c_str(), ios::in);     if(f_in.fail()) {         cerr << "error: param::param() failed open file \"" <<                 filepath.c_str() << "\".  exiting...\n";         exit(1);     }     for(int i=0; i<size; ++i) {         f_in.getline(line,100);         x[i]=strtod(line,null);     }     f_in.close(); } 

main.cpp

//main.cpp #include <iostream> #include "globals.h" int main(int argc, char** argv) {     param d("./myparam",3);     std::cout << d << "\n";     return 0; } 

myparam__lb

0.13 0.24 0.45 

myparam__mid

0.29 0.39 0.56 

myparam__ub

0.50 0.61 0.72 

the function ostream& operator<< (ostream &out, param &cparam) needs return statement. after adding return out;, well.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -