写代码中遇到的一个问题,楼主用的Dev C++


   #include <cstdlib>
    #include <iostream>
    using namespace std;
    int strlen(const char * s) 
    {    int i = 0;
        for(; s[i]; ++i);
        return i;
    }
    void strcpy(char * d,const char * s)
    {
        int i = 0;
        for( i = 0; s[i]; ++i)
            d[i] = s[i];
        d[i] = 0;
            
    }
    int strcmp(const char * s1,const char * s2)
    {
        for(int i = 0; s1[i] && s2[i] ; ++i) {
            if( s1[i] < s2[i] )
                return -1;
            else if( s1[i] > s2[i])
                return 1;
        }
        return 0;
    }
    void strcat(char * d,const char * s)
    {
        int len = strlen(d);
        strcpy(d+len,s);
    }
    class MyString
    {

 public:
        char* p;
        MyString(){
            p=NULL;
        };
        MyString(const char* s){
            if(s){
            int l=strlen(s);
            p=new char[l];
            strcpy(p,s);}
            else p=NULL;
        }
     friend int operator>(MyString a,MyString b){
    int l=strcmp(a.p,b.p);
        if(l==1) return 1;
        return 0;
    }
    friend int operator<(MyString a,MyString b){
    int l=strcmp(a.p,b.p);
        if(l==-1) return 1;
        return 0;
    }
     friend int operator==(MyString a,MyString b){
        int l=strcmp(a.p,b.p);
        if(l==0) return 1;
        return 0;
    }
     friend  ostream& operator<<(ostream& o,MyString a){
         if(a.p)
        o<<a.p;
        return o;
    }
MyString operator=(MyString a){
    if(a.p){
        int l=strlen(a.p);
            p=new char[l];
            strcpy(p,a.p);}
            else p=NULL;
            return *this;
    }
friend MyString operator+(MyString a,MyString b){
    MyString c;
    c=a;
     strcpy(c.p,a.p);
         strcat(c.p,b.p);
         return c;
    }
 char& operator[](int n){
        return p[n];
    }
 
void  operator=(const char* s){
            int l=strlen(s);
            p=new char[l];
            strcpy(p,s);
    }
        void operator+=(const char* s){
            strcat(p,s);
        }
 friend MyString operator+(const char* a,MyString b){
 99     char* d;
  /*    char c;
      c='A';*/
      strcpy(d,a);
       strcat(d,b.p);
       b.p=d;
  101     return b;
  
       
    }
MyString operator+(const char* a){
        
         strcat(p,a);
         return *this;
         }
    char operator()(int a,int b){
    
        for(int i=a;i<=a+b-1;i++){
            if(i!=a+b-1)
              cout<<p[i];
              else return p[i];}
    }
    
    };


   int CompareString( const void * e1, const void * e2)
    {
        MyString * s1 = (MyString * ) e1;
        MyString * s2 = (MyString * ) e2;
        if( * s1 < *s2 )
        return -1;
        else if( *s1 == *s2)
        return 0;
        else if( *s1 > *s2 )
        return 1;
    }
    int main()
    {
        MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
        MyString SArray[4] = {"big","me","about","take"};
        cout << "1. " << s1 << s2 << s3<< s4<< endl;
        s4 = s3;
        s3 = s1 + s3;
        cout << "2. " << s1 << endl;
        cout << "3. " << s2 << endl;
        cout << "4. " << s3 << endl;
        cout << "5. " << s4 << endl;
        cout << "6. " << s1[2] << endl;
        s2 = s1;
        s1 = "ijkl-";
        s1[2] = 'A' ;
        cout << "7. " << s2 << endl;
        cout << "8. " << s1 << endl;
        s1 += "mnop";
        cout << "9. " << s1 << endl;
        *  s4 = "qrst-" + s2;
        cout << "10. " << s4 << endl;
        s1 = s2 + s4 + " uvw " + "xyz";
        cout << "11. " << s1 << endl;
        qsort(SArray,4,sizeof(MyString),CompareString);
        for( int i = 0;i < 4;i ++ )
        cout << SArray[i] << endl;
        //s1的从下标0开始长度为4的子串
        cout << s1(0,4) << endl;
        //s1的从下标5开始长度为10的子串
        cout << s1(5,10) << endl;
  
    
        return 0;
    }

上面程序中99至101行(已标注)如果没有注释的两行则在main函数中加*行的语句无法执行,是为什么?

【若能帮到您,望给个采纳该答案,谢谢】
1、需要先理解char 和 char的区别*

区别存储数据类型
char*字符串指针数据类型
char一个字符字符类型

2、效果如下
解决方法:将单引号改为双引号,双引号表示字符串,单引号表示一个字符

img