梵洛何 2015-03-27 07:18 采纳率: 18.2%
浏览 1532

string类自己定义的,怎么一直有错啊,求指教

调试时一直显示无法读取字符串中的字符,怎么办啊,哪里出了问题

 #include<stdio.h>
class String
{
    char * p;
public:
    String(const char *p1=NULL);
    String(String & s);
    String& operator =(String & s);
    String operator +(String & s);
    bool operator ==(String & s);
    bool operator <(String & s);
    bool operator >(String & s);
    ~String();
};


 #include<iostream>
#include"string.h"
using namespace std;
String::String(const char *p1)
{
    if(p1==NULL)
    {p=new char[1];
    *p='\0';
    }
    else
    {
     int len=strlen(p);
     p=new char[len+1];
     strcpy(p,p1);
    }
}
String::String(String &s)
{
    int len=strlen(s1.p);
    p=new char[len+1];
    strcpy(p,s1.p);
}
String& String::operator =(String &s)
{
    delete []p;
    int len=strlen(s2.p);
    p=new char[len+1];
    strcpy(p,s2.p);
    return *this;
}
String   String::operator +(String &s)
{
    int len1=strlen(s3.p);
    int len2=strlen(p);
    String temp;
    temp.p= new char[len1+len2+1];
    strcpy(temp.p,p);
    strcat(temp.p,s3.p);
    return temp;
}
bool  String::operator==(String & s)
{
    if(strcmp(p,s.p)==0)
        return true;
    else return false;
}
bool String::operator>(String & s)
{
    if(strcmp(p,s.p)>0)
        return true;
    else return false;
}
bool String::operator<(String &s)
{
    if(strcmp(p,s.p)<0)
        return true;
    else return false;
}
String ::~String()
{
    delete []p;
    p=NULL;
}


#include"string.h"
using namespace std;
int main()
{
    String  s1;
    return 0;
}
  • 写回答

3条回答 默认 最新

  • oyljerry 2015-03-27 07:59
    关注

    String s1;

    你初始化时,什么字符串都没有赋值

    评论

报告相同问题?

悬赏问题

  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素