简体   繁体   English

通过运算符重载C ++进行字符串连接

[英]String concatenation by operator overloading C++

I am Unable to concatenate two strings by operator overloading. 我无法通过运算符重载来连接两个字符串。 The code is given below. 代码如下。

  #include<iostream>
  #include<string.h>
  using namespace std;
  class String
  {
  char *len;
  public:
         String(char *s);
         void display();
         String(){}
         void setData(char *s);
         //String(String &x); 
         ~String(); 
         String operator+(String);
         void extend(int l);         
   };
   void String::extend(int f)
   {
       len=(char *)realloc(len,(f+1)*sizeof(char));
   }     
   String String::operator+(String x)
   {
     String t;
     printf("%s\n",len);
     t.setData(len);
     t.extend(strlen(len)+strlen(x.len));
     strcat(t.len,x.len);
     printf("%s\n",t.len);
     return (t);

    }       
   String::~String()
   {
       delete len;  

    }                 

  void String::setData(char *s)
  {

      len=(char *)calloc(strlen(s)+1,sizeof(char));
      strcpy(len,s);
   }     
   String::String(char *s)
   {   
       len=(char *)calloc(strlen(s)+1,sizeof(char));                
       strcpy(len,s);
   }
   void String::display()
   {
        printf("%s\n",len);
    }
  int main()
  {

    String a=String("United ");
    String b,c;
    b.setData("States");
    c=a+b;
    c.display();
    system("pause");
    return 0;
   }                                       

The Problem is that string is being concatenated inside the operator overloading function but when the object is returned and when display function is invoked then the output is garbage value. 问题在于字符串是在运算符重载函数内部串联的,但是当返回对象并调用显示函数时,输出为垃圾值。

Your code doesn't follow Rule Of Three . 您的代码未遵循“三则规则” You should manually overload copy-constructor and assignment operator. 您应该手动重载复制构造函数和赋值运算符。 Also, you SHOULN'T use delete on buffer, that was allocated by malloc / calloc / realloc , use free instead. 另外,您不应在由malloc / calloc / realloc分配的缓冲区上使用delete ,而应使用free

Use 采用

<string> 

which is c++ library in place of 这是c ++库代替

<string.h> 

which is ac library. 这是一个交流图书馆。

Able to figure out what went wrong, the definition of destructor was acting as a bugaboo. 能够找出问题出在哪里,析构函数的定义就像一个bugaboo。 As soon as the operator function goes out of scope destructor is called and object is destroyed. 一旦运算符函数超出范围,就会调用析构函数,并销毁对象。

This code should work fine here. 此代码在这里应该可以正常工作。 I know its late , but still. 我知道已经晚了,但仍然。

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

class String
{
    char x[40];

    public:
    String() { }          // Default Constructor

    String( char s[] )
    {
        strcpy(x,s);
    }

    String( String & s )
    {
        strcpy(x,s.x );
    }

    String operator + ( String s2 )
    {
        String res;

        strcpy( res.x,x );
        strcat( res.x,s2.x);

        return(res);
    }

    friend ostream & operator << ( ostream & x,String & s );
};

ostream & operator << ( ostream & x,String & s )
{
    x<<s.x;
    return(x);
}

int main()
{
    clrscr();

    String s1="Vtu";
    String s2="Belgaum";

    String s3 = s1+ s2;      

    cout<<"\n\ns1 = "<<s1;
    cout<<"\n\ns2 = "<<s2;

    cout<<"\n\ns1 + s2 = "<<s3;

    getch();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM