求帮助,c++程序,可能问题出在指针或是析构函数上,望各位大神指点

weixin_38910147 2017-05-26 03:37:17
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Mystring
{
private:
char *pstr;
int length;
public:
Mystring(char*s);
Mystring(Mystring &mystring);
~Mystring();
Mystring operator+=(const Mystring &str);
Mystring operator=(const Mystring &string);
friend bool operator== (const Mystring& string1, const Mystring& string2);
friend bool operator!= (const Mystring& string1, const Mystring& string2);
friend bool operator> (const Mystring& string1, const Mystring& string2);
friend bool operator< (const Mystring& string1, const Mystring& string2);
char& operator[](int n);
friend ostream &operator<<(ostream &os, const Mystring &string);
friend istream &operator >> (istream &is, Mystring &string);
};

Mystring::Mystring(char * s)
{
length = strlen(s);
pstr = new char[length + 1];
strcpy(pstr, s);
}

Mystring::Mystring(Mystring & mystring)
{
cout << "调用拷贝构造函数" << endl;
this->pstr = new char[mystring.length + 1];
strcpy(pstr, mystring.pstr);
length = mystring.length;
}

Mystring::~Mystring()
{
delete pstr;
cout << "调用析构函数" << endl;
system("pause");
}

Mystring Mystring::operator+=(const Mystring & string)
{
int n = length;
length += string.length;
char* temp = new char[length + 1];
int i;
for (i = 0; i <= length; i++)
{
if (i<n)
{
temp[i] = pstr[i];
}
else if (i >= n)
{
temp[i] = string.pstr[i - n];
}
}
delete pstr;
pstr = new char[length + 1];
strcpy(pstr, temp);
cout << "调用+=重载" << endl;
return *this;
}

Mystring Mystring::operator=(const Mystring & string)
{
delete this->pstr;
this->pstr = new char[string.length + 1];
strcpy_s(this->pstr, string.length, string.pstr);
this->length = string.length;
return *this;
}

bool operator==(const Mystring & string1, const Mystring & string2)
{
return (string1.pstr == string2.pstr);
}

bool operator!=(const Mystring & string1, const Mystring & string2)
{
return !(string1.pstr == string2.pstr);
}

bool operator>(const Mystring & string1, const Mystring & string2)
{
return (string1.pstr > string2.pstr);
}

bool operator<(const Mystring & string1, const Mystring & string2)
{
return (string1.pstr < string2.pstr);
}

char & Mystring::operator[](int n)
{
static char ch = 0;
if (n > length || n < 0)
{
cout << "Out of the bond!" << endl;
return ch;
}
else
return *(pstr + n);
}

ostream & operator<<(ostream & os, const Mystring & string)
{
os << "String: " << string.pstr << '\t' << " Length: " << string.length;
return os;
}

istream & operator >> (istream & is, Mystring & string)
{
delete string.pstr;
is >> string.pstr;
string.length = strlen(string.pstr);
return is;
}

int main()
{
Mystring mystring1("hdjs");
cout << mystring1 << endl;
Mystring mystring2("shddsi");
cout << mystring2 << endl;
cout << "请重新为mystring2输入:";
cin >> mystring2;
cout << mystring2 << endl;
mystring1 += mystring2;
cout << "连接两个字符串:";
cout << mystring1 << endl;
system("pause");
return 0;
}
该程序并不是完整代码,但不影响运行。
结果有误,望指点。
...全文
122 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
幻夢之葉 2017-05-26
  • 打赏
  • 举报
回复
改了一些,你对比下 这个类的设计问题颇多

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Mystring
{
private:
    char *pstr;
    int length;
public:
    Mystring(char*s);
    Mystring(Mystring &mystring);
    ~Mystring();
    // 返回值修改为Mystring&
    Mystring& operator+=(const Mystring &str);
    Mystring& operator=(const Mystring &string);
    
    friend bool operator== (const Mystring& string1, const Mystring& string2);
    friend bool operator!= (const Mystring& string1, const Mystring& string2);
    friend bool operator> (const Mystring& string1, const Mystring& string2);
    friend bool operator< (const Mystring& string1, const Mystring& string2);
    char& operator[](int n);
    char operator[](int n) const;
    friend ostream &operator<<(ostream &os, const Mystring &string);
    friend istream &operator >> (istream &is, Mystring &string);
};

Mystring::Mystring(char * s)
{
    length = strlen(s);
    pstr = new char[length + 1];
    strcpy(pstr, s);
}

Mystring::Mystring(Mystring & mystring)
{
    cout << "调用拷贝构造函数" << endl;
    this->pstr = new char[mystring.length + 1];
    strcpy(pstr, mystring.pstr);
    length = mystring.length;
}

Mystring::~Mystring()
{
    delete pstr;
    cout << "调用析构函数" << endl;
    system("pause");
}

Mystring& Mystring::operator+=(const Mystring & string)
{
    int n = length;
    length += string.length;
    char* temp = new char[length + 1];
    int i;
    for (i = 0; i <= length; i++)
    {
        if (i < n)
        {
            temp[i] = pstr[i];
        }
        else if (i >= n)
        {
            temp[i] = string.pstr[i - n];
        }
    }
    delete pstr;
    //pstr = new char[length + 1];
    //strcpy(pstr, temp);
    pstr = temp;
    this->length = length;
    cout << "调用+=重载" << endl;
    return *this;
}

Mystring& Mystring::operator=(const Mystring & string)
{
    // 增加自我赋值的判断
    if (this == &string)
        return *this;

    delete this->pstr;
    this->pstr = new char[string.length + 1];
    strcpy_s(this->pstr, string.length + 1, string.pstr);
    this->length = string.length;
    return *this;
}

bool operator==(const Mystring & string1, const Mystring & string2)
{
    //return (string1.pstr == string2.pstr);
    return strcmp(string1.pstr, string2.pstr) == 0;
}

bool operator!=(const Mystring & string1, const Mystring & string2)
{
    //return !(string1.pstr == string2.pstr);
    return strcmp(string1.pstr, string2.pstr) != 0;
}

bool operator>(const Mystring & string1, const Mystring & string2)
{
    //return (string1.pstr > string2.pstr);
    return strcmp(string1.pstr, string2.pstr) > 0;
}

bool operator<(const Mystring & string1, const Mystring & string2)
{
    //return (string1.pstr < string2.pstr);
    return strcmp(string1.pstr, string2.pstr) < 0;
}

char & Mystring::operator[](int n)
{
    static char ch = 0;
    if (n >= length || n < 0)
    {
        cout << "Out of the bond!" << endl;
        return ch;
    }
    else
        return *(pstr + n);
}

char Mystring::operator[](int n) const
{
    static char ch = 0;
    if (n >= length || n < 0)
    {
        cout << "Out of the bond!" << endl;
        return ch;
    }
    else
        return *(pstr + n);
}


ostream & operator<<(ostream & os, const Mystring & string)
{
    os << "String: " << string.pstr << '\t' << "  Length: " << string.length;
    return os;
}

istream & operator >> (istream & is, Mystring & string)
{
    // 最多输入2048长度的字符串,在真实情况这里是不可限定的
    char *tmp = new char[2048];
    is >> tmp;

    delete string.pstr;

    int len = strlen(tmp);
    string.pstr = new char[len + 1];
    strcpy(string.pstr, tmp);
    string.length = len;

    return is;
}

int main()
{
    Mystring mystring1("hdjs");
    cout << mystring1 << endl;
    Mystring mystring2("shddsi");
    cout << mystring2 << endl;
    cout << "请重新为mystring2输入:";
    cin >> mystring2;
    cout << mystring2 << endl;
    mystring1 += mystring2;
    cout << "连接两个字符串:";
    cout << mystring1 << endl;
    system("pause");
    return 0;
}
wallesyoyo 2017-05-26
  • 打赏
  • 举报
回复
1. 首先如楼上赵老师所说,一定要学会调试,如果还是不会,上网搜一下,这个是基本技能。 2. 你的>>运算符重载函数,里面delete一段内存,然后又往这段内存写数据,按你描述你的代码很不幸地没有崩在这,在程序结束时析构mystring2时,再次delete这段已经被delete的内存时崩了,而你并不知道是怎么回事。所以,在delete掉一段内存后马上把指针赋空是多么的重要,如果你那样做,程序会直接在运算符重载函数里面崩溃,然后会马上找到崩溃原因。
lzm_cn01 2017-05-26
  • 打赏
  • 举报
回复
istream & operator >> (istream & is, Mystring & string)
{
    delete string.pstr;
    is >> string.pstr;
    string.length = strlen(string.pstr);
    eturn is;
}
代码问题很多,这里你delete调了 string.str,指针是没有内存分配的,再用流往里面写东西的话会把内存搞乱的,后面就崩溃了。
赵4老师 2017-05-26
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧