2012-03-07 40 views
0

我正在检查测试,并且正在处理我的个人项目,但我正在进行增量开发。 我想在我的大学学习中做得很好。为什么我的输出流seg错误和我的虚拟析构函数不起作用,但是当我终止虚拟时它确实是

它在我的ostream操作符seg错误,我的虚拟功能不工作,除非它与虚拟。

#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;} 

回答

2

operator<<(),作为实现,是一个无限递归调用:

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

,并会导致堆栈溢出。

我想你的意思是:

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

我认为这将工作更好不知道虽然(我想数据是一个0终止的C字符串)。

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

由于它的工作。我是一名学生,我想我可能需要找到一位导师时常工作。我认为和我的教授不好,因为他看起来很自负。 – Cferrel 2012-03-07 22:24:05

相关问题