免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6957 | 回复: 9

[C++] 在VC6.0中写了一个简单的String类,求高手帮我除错 [复制链接]

论坛徽章:
0
发表于 2009-11-26 11:52 |显示全部楼层
10可用积分
能编译过去,就是运行出错。加了调试语句后,发现String d = b+c之后就出错了。


#include <iostream.h>
#include <string.h>

class String
{
public:
    String();
    String(const char * src);
    String(String & src);
    ~String();
    void operator = (String sub);
    String operator + (String sub);

    char * ToString();
    int GetLen();
private:
    char * str;
    int len;
};

String::String()
{
    len = 0;
    str = NULL;
}


String::String(const char * src)
{
    if(src == NULL)
        return;
    len = strlen(src);
    str = new char(len+1);
    strcpy(str,src);
}

String::String(String & src)
{
    len = src.GetLen();

    cout<<"src的长度是:"<<len<<endl;
    cout<<"src的内容是:"<<src.ToString()<<endl;

    if(len != 0)
    {
        str = new char(len+1);
        strcpy(str,src.ToString());
        cout<<"内容拷贝成功"<<endl;
    }
}

String::~String()
{
    delete []str;
    str = NULL;
    len = 0;
}

int String::GetLen()
{
    return len;
}

char * String::ToString()
{
    return str;
}

void String::operator = (String other)
{
    int sublen = other.GetLen();
    if(sublen != 0)
    {
        str = new char(len+1);
        strcpy(str,other.ToString());
        len = sublen;
    }
}

String String::operator + (String sub)
{
    cout<<"phase 1"<<endl;

    int newLen=len+sub.GetLen();
    char * newstr = new char(newLen+1);

    strcpy(newstr,str);
    strcat(newstr,sub.ToString());

    cout<<"phase 2"<<endl;

    String newString(newstr);

    cout<<"phase 3"<<endl;

    return newString;
}


void main()
{
    String a;
    String b("hello world");
    cout<<"b的长度是:"<<b.GetLen()<<endl;
    cout<<"b的内容是:"<<b.ToString()<<endl;

    String c = b;
    cout<<"c的长度是:"<<c.GetLen()<<endl;
    cout<<"c的内容是:"<<c.ToString()<<endl;

    String d = b+c;

    cout<<"d的长度是:"<<d.GetLen()<<endl;
    cout<<"d的内容是:"<<d.ToString()<<endl;
}



[ 本帖最后由 也是菜鸟 于 2009-11-26 11:55 编辑 ]

最佳答案

查看完整内容

new char(len+1)改成new char[len+1]

论坛徽章:
0
发表于 2009-11-26 11:52 |显示全部楼层
new char(len+1)改成new char[len+1]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2009-11-26 12:40 |显示全部楼层
单步跟踪调试会不会?

论坛徽章:
0
发表于 2009-11-26 13:38 |显示全部楼层
原帖由 lenovo 于 2009-11-26 12:40 发表
单步跟踪调试会不会?

step into     or     step over   ?
我跟了啊,就是d=b+c出错的。这条语句的执行顺序是:调用拷贝构造函数-->调用+重载函数--->调用拷贝构造函数,
第二次调用拷贝构造函数是出错了,就是return newString;出问题的。

可是不明白它为什么要出错(我在看看书先。。。。)

论坛徽章:
0
发表于 2009-11-26 13:39 |显示全部楼层
原帖由 emacsnw 于 2009-11-26 13:12 发表
new char(len+1)改成new char[len+1]

真的啊,这个是个问题。
就是这个造成的。。。现在程序没问题了。

谢谢两位的回答

[ 本帖最后由 也是菜鸟 于 2009-11-26 13:42 编辑 ]

论坛徽章:
1
双子座
日期:2014-08-29 17:15:03
发表于 2009-11-27 10:51 |显示全部楼层
记得去像素软件面试,还给写了个String类,浪费时间阿。

建议楼主注意编码规范。

论坛徽章:
0
发表于 2011-07-04 13:48 |显示全部楼层
请教彩色的代码是如何弄的?

论坛徽章:
0
发表于 2011-07-06 15:42 |显示全部楼层
lz写的相当好,学习啦啊,不过我把原来有问题的地方修改了下:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class String
  5. {
  6. public:
  7.         String();
  8.         String(const char * src);
  9.         String(String & src);
  10.         ~String();
  11.         void operator = (String& sub);
  12.         String operator + (String sub);

  13.         char * ToString();
  14.         int GetLen();
  15. private:
  16.         char * str;
  17.         int len;
  18. };

  19. String::String()
  20. {
  21.         len = 0;
  22.         str = NULL;
  23. }


  24. String::String(const char * src)
  25. {
  26.         if(src == NULL)
  27.                 return;
  28.         len = strlen(src);
  29.         str = new char[len+1];
  30.         strcpy(str,src);
  31. }

  32. String::String(String & src)
  33. {
  34.         len = src.GetLen();

  35.         cout<<"src的长度是:"<<len<<endl;
  36.         cout<<"src的内容是:"<<src.ToString()<<endl;

  37.         if(len != 0)
  38.         {
  39.                 str = new char[len+1];
  40.                 strcpy(str,src.ToString());
  41.                 cout<<"内容拷贝成功"<<endl;
  42.         }
  43. }

  44. String::~String()
  45. {
  46.         delete []str;
  47.         str = NULL;
  48.         len = 0;
  49. }

  50. int String::GetLen()
  51. {
  52.         return len;
  53. }

  54. char * String::ToString()
  55. {
  56.         return str;
  57. }

  58. void String::operator = (String& other)
  59. {
  60.         int sublen = other.GetLen();
  61.         if(sublen != 0)
  62.         {
  63.                 str = new char[len+1];
  64.                 strcpy(str,other.ToString());
  65.                 len = sublen;
  66.         }
  67. }

  68. String String::operator + (String sub)
  69. {
  70.         cout<<"phase 1"<<endl;

  71.         int newLen=len+sub.GetLen();
  72.         char * newstr = new char[newLen+1];

  73.         strcpy(newstr,str);
  74.         strcat(newstr,sub.ToString());

  75.         cout<<"phase 2"<<endl;

  76.         String newString(newstr);

  77.         cout<<"phase 3"<<endl;

  78.         return newString;
  79. }


  80. void main()
  81. {
  82.         String a;
  83.         String b("hello world");
  84.         cout<<"b的长度是:"<<b.GetLen()<<endl;
  85.         cout<<"b的内容是:"<<b.ToString()<<endl;

  86.         String c = b;
  87.         cout<<"c的长度是:"<<c.GetLen()<<endl;
  88.         cout<<"c的内容是:"<<c.ToString()<<endl;

  89.         String d = b+c;

  90.         cout<<"d的长度是:"<<d.GetLen()<<endl;
  91.         cout<<"d的内容是:"<<d.ToString()<<endl;
  92. }
复制代码

论坛徽章:
0
发表于 2013-08-09 09:50 |显示全部楼层
你们用的是哪个版本的VC++6.0啊,我的压根就不能用#include <iostream>、using namespace std、#include <string>等等,只能用带.h的头文件,为什么啊?求指导!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP