(-_-) zzz967 2024-07-09 16:59 采纳率: 50%
浏览 3

类的初始化和拷贝构造或者重载赋值难道不能同时进行吗?

#in

#ifndef STRING_H
#define STRING_H

#include<cstring>
#include<iostream>
using namespace std;
class String
{
    public:
        String();
     ~String();
     void clear();
     String(const String &copy1);
     String(const char * copy);
     void operator =(const String &copy1);
     void operator =(const char*copy);
     const char *c_str()const;

    protected:
        char *entries;
        int length;
};
bool operator ==(const String &first,const String &second);
bool operator >(const String &first,const String &second);
bool operator <(const String &first,const String &second);
bool operator >=(const String &first,const String &second);
bool operator <=(const String &first,const String &second);
bool operator !=(const String &first,const String &second);
void strcat(String &add_to,const String &add_on);
String operator + (const String &text,const String &target);
void write(String &s);
int strstr(const String &text,const String &target);
ostream & operator<<(ostream &output,const String &s);
#endif // STRING_H


```tput;
}
1. 
```c++
#include "String.h"
     String::String(){
     entries=NULL,length=0;
     }
     String::~String(){
     delete [] entries;
     entries=NULL;
     }
    String:: String(const String &copy1){
          const char *copy=copy1.c_str();
        int x=strlen(copy1.entries);
    if(entries!=NULL) clear(); 
     entries=new char[x+1];
     length=x;
     strcpy(entries,copy);
    }
void String::clear(){
    delete [] entries;
    entries=NULL;
    length=0;
}
    String:: String(const char * copy){
     int x=strlen(copy);
        if(entries!=NULL) clear(); 
     entries=new char[x+1];
     length=x;
     strcpy(entries,copy);
    }
  void String:: operator=(const String &copy1){
     if (this!= &copy1) {
               if(entries!=NULL) clear();
            entries = new char[strlen(copy1.entries) + 1];
            strcpy(entries,copy1.entries);
        }
    }
  void String:: operator=(const char*copy){
        int x=strlen(copy);
        if(entries!=NULL) clear(); 
     entries=new char[x+1];
     length=x;
         strcpy(entries,copy);
    }
    const char * String:: c_str()const{
    return entries;
    }


bool operator ==(const String &first,const String &second){
    const char *a=first.c_str(), *b=second.c_str();
int c=strcmp(a,b);
if(c==0) return 1;
else return 0;
}
bool operator >(const String &first,const String &second){
const char *a=first.c_str(), *b=second.c_str();
int c=strcmp(a,b);
if(c>0) return 1;
else return 0;
}
bool operator <(const String &first,const String &second){
const char *a=first.c_str(),*b=second.c_str();
int c=strcmp(a,b);
if(c<0) return 1;
else return 0;
}
bool operator >=(const String &first,const String &second){
const char *a=first.c_str(), *b=second.c_str();
int c=strcmp(a,b);
if(c>=0) return 1;
else return 0;
}
bool operator <=(const String &first,const String &second){
    const char *a=first.c_str(),*b=second.c_str();
int c=strcmp(a,b);
if(c<=0) return 1;
else return 0;
}
bool operator !=(const String &first,const String &second){
const char *a=first.c_str(), *b=second.c_str();
int c=strcmp(a,b);
if(c!=0) return 1;
else return 0;
}
void strcat(String &add_to,const String &add_on){
const char *a=add_to.c_str(),*b=add_on.c_str();
char*c=new char[strlen(a)+strlen(b)+1];
strcat(c,a);
strcat(c,b);
add_to=c;
}
String operator + (const String &text,const String &target){
String c;
c="";
strcat(c,text);
strcat(c,target);
return c;
}
void write(String &s){
cout<<s.c_str();}
int strstr(const String &text,const String &target){
 const char *a=text.c_str(),*b=target.c_str();
int c=strstr(a,b)-a;
return c; 
}
ostream & operator<<(ostream &output,const String &s){
output<<s.c_str();
return output;
}


#include"String.cpp"

int main()
{String s1="s1 string";
cout<<s1.c_str()<<endl;
cout<<s1;
cout<<strlen(s1.c_str())<<endl;
String s2("s2 string");
cout<<s2.c_str()<<endl;
cout<<s2;
cout<<strlen(s2.c_str())<<endl;
cout<<s1+s2;
if(s1>=s2) cout<<"s1>=s2"<<endl;
else cout<<"s1<s2"<<endl;
String s3="s3 string";
strcat(s3,s1);
write(s3);
String text="This is text.";
String target="text";
cout<<strstr(text,target)<<endl;
cin.get();
return 0;
}

为什么输出不了,而且一直显示 3221226356;
但是如果把输入只用一个,比如只s1,或s2,却能出出正确的。

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-07-09 16:59
    关注

    引自免费微信小程序:皆我百晓生

    这个问题是关于C++中的字符串操作符+=+。在标准库函数中,字符串连接(+)通常被实现为一个无参成员函数,并且它不会修改原始对象,而是返回一个新的字符串。

    然而,在您提供的示例中,您尝试通过调用operator=将两个字符串连接起来。这会导致编译器错误,因为operator=是一个赋值运算符,而不是一个连接字符串的函数。正确的做法应该是将这两个字符串作为参数传递给+函数,然后在连接后使用+返回新的字符串。

    因此,您的代码应该像这样修改:

    String s1 = "s1 string";
    String s2 = "s2 string";
    
    // 连接字符串
    String s3 = s1 + s2;
    

    这是正确的方法来处理字符串连接的操作。

    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 7月9日
  • 创建了问题 7月9日