Thread: problems with overloaded '+' again

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    Angry problems with overloaded '+' again

    Ok look , i have a simple string class with overloaded '+=' , '=' and '+'. The '+=' and '=' work just fine when i use them with object. But the friggin '+' sign is still causing troubles !!!

    I spent like 1 hour on this crap and i still can't figure out how to get around this "Debug Assertion Failed" error (vc++). I tracked execution and found that the problem occurs when one of the objects (don't know which) tries to delete it's sPtr space.

    anyway , the following are my .h , .cpp and main.cpp files. The problem is probably in the operator+ function (hilihgted in blue) , i posted the whole code just incase you need to take a look at something else :

    mystr.h
    Code:
    #ifndef MYSTR_H
    #define MYSTR_H
    
    class mystr
    {
    public :
    	mystr(const char * = "");
    	~mystr();
    	
    	const mystr &operator+=(const mystr &);
    	const mystr &operator=(const mystr &);
    
    	mystr operator+(const mystr &);
    
    	void print();
    
    private :
    	int length;
    	char *sPtr;
    };
    
    #endif
    mystr.cpp
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    #include <cstring>
    #include "mystr.h"
    
    mystr::mystr(const char *s) // default constructor
    {
    	length = strlen(s);
    	sPtr = new char[ length + 10 ];
    
    	strcpy(sPtr, s);
    }
    
    
    mystr::~mystr()   // destructor
    {
    	delete [] sPtr;
    }
    
    
    const mystr &mystr::operator +=(const mystr &right)
    {
    	size_t newLength = length + right.length;
    
    	char *tempPtr = new char[newLength + 1];
    
    	strcpy(tempPtr, sPtr);
    	strcpy(tempPtr + length, right.sPtr);
    	
    	delete [] sPtr;
    
    	sPtr = tempPtr;
    	length = newLength;
    
    	return *this;
    }
    
    
    mystr mystr::operator+(const mystr &right)
    {
    	mystr temp(sPtr);
    
    	temp += right;
    
    	return temp;
    
    }
    
    
    const mystr &mystr::operator=(const mystr &right)
    {
    	if( &right != this)
    	{
    		delete [] sPtr;
    
    		sPtr = new char[right.length + 1];
    
    		strcpy(sPtr, right.sPtr);
    	}
    
    	return *this;
    }
    
    
    
    void mystr::print()
    {
    	cout << sPtr << endl;
    }
    main.cpp
    Code:
    #include "mystr.h"
    
    int main(void)
    {
    	mystr a("hello"), b("world\n"), c;
    
    	a.print();
    
    	b.print();
    
    	c = a + b;
    
    	c.print();
    
    	return 0;
    }

    it only displays :

    hello
    world

    then shows an error. I hope someone can help before i tear the rest of my hair out.

    p.s : please don't give examples , just show me how to fix my my code.
    Last edited by Brain Cell; 04-13-2005 at 06:12 PM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    I've had the same problem before too....The problem's in that when returning the temporary string, the destructor gets called, but AFTER the string is returned, so you end up returning a string with an old, dead pointer value that's just been deleted!

    *pulls hair out*

    The only thing I can think of is a method which involves creating, dynamically, a new string and returning the pointer to it....but this will end up creating a memory leak

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your code seems fine, you just need to add a copy constructor.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Ah hah! So it's just a matter of using the same copy constructor as the overloaded operator is using? As in, in this case, the + operator's using a mystr&, so use a copy constructor that accepts a mystr&

    That's what I just did in the above code and the crash disappeared.

    Code:
    mystr::mystr(const mystr &s)
    {
    	length=s.length;
    	sPtr = new char[ length + 1 ];
    
    	strcpy(sPtr, s.sPtr);
    }
    Code:
    mystr mystr::operator+(const mystr &right)
    {
    	mystr temp(*this);
    
    	temp += right;
    
    	return temp;
    }
    Oh, nevermind, the copy constructor's called when the return statement is hit.
    Last edited by jverkoey; 04-13-2005 at 07:27 PM.

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    yea i just need to add a copy constructor to the original code. Thanks alot Daved and Jeff. If you were girls , i'd fall in love with you both
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Brain Cell
    I spent like 1 hour on this crap and i still can't figure out how to get around this "Debug Assertion Failed" error (vc++). I tracked execution and found that the problem occurs when one of the objects (don't know which) tries to delete it's sPtr space.
    Why? I told you yesterday that you needed a copy constructor.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Remember the rule of three: If you implement either the copy constructor, destructor or assignment operator, you should probably implement all three.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Please remember to keep threats of violence out of your thread titles.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    This is how I implemented my + operator for my little string class
    Code:
    //.h
    friend string operator+(const string &s, const string &s2);
    friend string operator+(const char *s, const string &s2);
    friend string operator+(const char ch, const string &s2);
    friend string operator+(const string &s, const char *s2);
    friend string operator+(const string &s, const char ch);
    Code:
    //.cpp
        string operator+(const string &s, const string &s2)
        {
            string both = s;
            both += s2;
            return both;
        }
        string operator+(const char *s, const string &s2)
        {
            string both = s;
            both += s2;
            return both;
        }
        string operator+(const char ch, const string &s2)
        {
            string both(1,ch);
            both += s2;
            return both;
        }
        string operator+(const string &s, const char *s2)
        {
            string both = s;
            both += s2;
            return both;
        }
        string operator+(const string &s, const char ch)
        {
            string both = s;
            both += ch;
            return both;
        }
    Woop?

  10. #10
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Quote Originally Posted by pianorain
    Why? I told you yesterday that you needed a copy constructor.
    ooh , sorry. Seems like i skipped that line


    Quote Originally Posted by joshdick
    Remember the rule of three: If you implement either the copy constructor, destructor or assignment operator, you should probably implement all three.
    Useful tip , thanks .

    Quote Originally Posted by Thantos
    Please remember to keep threats of violence out of your thread titles.
    ummm...ok. But you knew i wasn't serious right ? i was only expressing how ***** off i was.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Overloaded = operator problems
    By Michty in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2006, 05:57 PM
  3. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  4. Problems with overloaded '+' and '='
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2005, 11:15 AM
  5. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 02:14 AM