c++ - Why is my output stream seg faulting and my virtual destructor does not work but when i kill virtual it does -
i reviewing test , working on personal project doing incremental development. want in university studies.
it seg faults on ostream operator , virtual function not work unless out virtual .
#include"mystring.h" mystring::mystring( ) //constructor { size=0; capacity=1; data=new char[capacity]; } mystring::mystring(char * n) //copy constructor { size=strlen(n); capacity=strlen(n)+1; data=new char[capacity]; strcpy(data,n); } mystring::mystring(const mystring &right) // { size=strlen(right.data); capacity=strlen(right.data)+1; data=new char [capacity]; strcpy(data,right.data); } mystring::~mystring( ) { delete [] data; } mystring mystring::operator = (const mystring& s) { if(this!=&s) { mystring temp=data; delete [] data; size=strlen(s.data); capacity=size+1; data= new char [capacity]; strcpy(data,s.data); } } mystring& mystring::append(const mystring& s) { if(this!=&s) { strcat(data,s.data); } } mystring& mystring::erase() { } mystring mystring::operator + (const mystring&)const { } bool mystring::operator == (const mystring&) { } bool mystring::operator < (const mystring&) { } bool mystring::operator > (const mystring&) { } bool mystring::operator <= (const mystring&) { } bool mystring::operator >= (const mystring&) { } bool mystring::operator != (const mystring&) { } void mystring::operator += (const mystring&) { } char& mystring::operator [ ] (int) { } void mystring::getline(istream&) { } int mystring::length( ) const { return strlen(data); } ostream& operator<<(ostream& out, mystring& s){ out<<s; return out; } // int mystring::getcapacity(){return capacity;}
the operator<<()
, implemented, infinite recursive call:
ostream& operator<<(ostream& out, mystring& s){ out<<s; return out; }
and result in stack overflow.
i think meant:
ostream& operator<<(ostream& out, mystring& s){ out << s.data; return out; }
Comments
Post a Comment