簡體   English   中英

為什么我的輸出流seg錯誤並且我的虛擬析構函數不起作用,但是當我殺死虛擬機時卻可以

[英]Why is my output stream seg faulting and my virtual destructor does not work but when i kill virtual it does

我正在審查測試,並且正在從事我的個人項目,但我正在進行增量開發。 我想在大學學習中表現出色。

它會在我的ostream運算符上出現故障,並且除非沒有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;}

實現的operator<<()是無限遞歸調用:

ostream& operator<<(ostream& out, MyString& s){
    out<<s;
    return out;
}

並會導致堆棧溢出。

我想你的意思是:

ostream& operator<<(ostream& out, MyString& s){
    out << s.data;
    return out;
}

我認為雖然不確定(我認為數據是一個0終止的c字符串),它會更好地工作。

ostream& operator<<(ostream& out, MyString& s) {
    out<<s.data;
    return out;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM