为什么我的输出流seg出错,而我的虚拟析构函数不起作用,但当我杀死虚拟的时候,它起作用了
Why is my output stream seg faulting and my virtual destructor does not work but when i kill virtual it does
我正在复习测试,我正在进行我的个人项目,但我正在进行增量开发。我想在大学里取得好成绩。
它在我的ostream操作符上设置了错误,我的虚拟函数不起作用,除非它有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;}
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;
}
相关文章:
- 为什么在这种情况下不调用我的虚拟函数实现?
- 我是否应该在包含虚拟方法的类上使用'memcpy'?如果没有,如何替换它?
- 如果我有指向基类对象的指针,如何获取虚拟方法的地址?
- 为什么我的虚拟破坏者多次执行这些执行
- 我是否错过了什么,或者虚拟呼叫的性能并不像人们所说的那样糟糕
- 我如何确保从我的派生类中调用纯虚拟方法
- 为什么虚拟功能打破了我的铸造
- getPeername()在客户端在虚拟机上运行时返回我的本地主机地址
- 在这种情况下,我的派生类是否还需要一个虚拟析构函数
- 我可以使用覆盖非虚拟方法的方法吗?
- 当我的基类和派生类具有相同的函数时,它必须是虚拟的吗?
- 我在哪里可以找到C++有状态虚拟基础的良好解释
- 如何确定为我的程序保留了多少虚拟内存
- 为什么我的输出流seg出错,而我的虚拟析构函数不起作用,但当我杀死虚拟的时候,它起作用了
- 为什么我没有得到虚拟打印机的上下文?
- 我正在使用放置新功能和虚拟功能;为什么我的虚拟功能表有误?
- 我的基类中有一个虚拟析构函数和一个数组.我怎样才能让它发挥作用
- 为什么我不能继承虚拟基的构造函数?
- 使用非虚拟接口习语,可以/将我的非虚拟函数内联
- 子类QCompleter,虚拟槽在我的子类没有被调用