注册 登录
编程论坛 C++教室

请教一下,我认为这个程序是对的,为什么运行出错呀!谢谢各位了

chencuicui 发布于 2007-12-09 22:15, 497 次点击
#include <iostream.h>
#include <string.h>
class MyString
{
public:
    MyString() {m_data = NULL;}            
    MyString(const char *str);                    
    ~MyString();                    
    MyString operator-(const MyString &other);
    MyString operator=(const MyString &other);
    friend ostream &operator<<(ostream &stream, const MyString &str);
                        //重载输出运算符"<<"
private:
    char *m_data;    
};
#define NULL 0
MyString::~MyString()
{
    if(m_data)
        delete [] m_data;
    m_data = NULL;
}
MyString::MyString(const char *str)
{
    if(str == NULL)
    {
        m_data = new char[1];
        *m_data = '\0';
    }
    else
    {
        int length = strlen(str);
        m_data = new char[length+1];
        strcpy(m_data, str);
    
    }
}
MyString MyString::operator=(const MyString &other)
{  
    if(this==&other)
       return *this;
    delete m_data;
    m_data=new char[strlen(other.m_data)+1];
    strcpy(m_data,other.m_data);
    return *this;
        
}

MyString  MyString::operator-(const MyString &other)
{   int i,j,t=0,m;
0 回复
1