Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem encountered while doing mathematical operations on strings by overloaded '+'.

problem encountered while doing mathematical operations on strings by overloaded '+'.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    #include
    #include
    using namespace std;

    class st{

    char \*p;
    int len;
    
    public:
    	st(){len=0; p=0;}		//creates null string
    	st(const char \*s);		//creates string from arrays
    	st(const st &s);		//copy constructor
    	~st(){delete p;}		//destructor
    
    	//+ operator
    	friend st operator + (const st &, const st &);
    
    	//<= operator
    	friend int operator <= (const st &, const st &);
    	friend void show(const st);
    

    };

    st :: st(const char *s){
    len = strlen(s);
    p = new char[len+1];
    strcpy(p, s);
    //cout<

    This program works fine when i replace

    Quote:

    st string1, string2, string3;

    with

    Quote:

    st string1=s1, string2=s2, string3=s1+s3;

    so what's the problem, i learned in class that we can pass the values in the constructor by calling it explicitly, and (string1, string2, string3) & (s1, s2, s3) have same return type i.e.class st.
    So why isn't it working ?

    thank you

    V C 2 Replies Last reply
    0
    • T Tarun Jha

      #include
      #include
      using namespace std;

      class st{

      char \*p;
      int len;
      
      public:
      	st(){len=0; p=0;}		//creates null string
      	st(const char \*s);		//creates string from arrays
      	st(const st &s);		//copy constructor
      	~st(){delete p;}		//destructor
      
      	//+ operator
      	friend st operator + (const st &, const st &);
      
      	//<= operator
      	friend int operator <= (const st &, const st &);
      	friend void show(const st);
      

      };

      st :: st(const char *s){
      len = strlen(s);
      p = new char[len+1];
      strcpy(p, s);
      //cout<

      This program works fine when i replace

      Quote:

      st string1, string2, string3;

      with

      Quote:

      st string1=s1, string2=s2, string3=s1+s3;

      so what's the problem, i learned in class that we can pass the values in the constructor by calling it explicitly, and (string1, string2, string3) & (s1, s2, s3) have same return type i.e.class st.
      So why isn't it working ?

      thank you

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      To make this part of code working:

      st string1, string2, string3;
      string1 = s1;
      string2 = s2;
      

      you must implement the assignment operator for your class st.

      1 Reply Last reply
      0
      • T Tarun Jha

        #include
        #include
        using namespace std;

        class st{

        char \*p;
        int len;
        
        public:
        	st(){len=0; p=0;}		//creates null string
        	st(const char \*s);		//creates string from arrays
        	st(const st &s);		//copy constructor
        	~st(){delete p;}		//destructor
        
        	//+ operator
        	friend st operator + (const st &, const st &);
        
        	//<= operator
        	friend int operator <= (const st &, const st &);
        	friend void show(const st);
        

        };

        st :: st(const char *s){
        len = strlen(s);
        p = new char[len+1];
        strcpy(p, s);
        //cout<

        This program works fine when i replace

        Quote:

        st string1, string2, string3;

        with

        Quote:

        st string1=s1, string2=s2, string3=s1+s3;

        so what's the problem, i learned in class that we can pass the values in the constructor by calling it explicitly, and (string1, string2, string3) & (s1, s2, s3) have same return type i.e.class st.
        So why isn't it working ?

        thank you

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        As Victor Nijegorodov noted, your code violates the rule of three[^]: you have to define the assignment operator.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups