strange error at ending of main

hi
there is some kind of "Debug Error!" when i debug my program

it's just a simple "MyString" class

MyString.h - MyString class declaration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//MyString class declaration
//using constructor, copy constructor, destructor

#include "stdafx.h"

#ifndef MYSTRING_H
#define MYSTRING_H

class MyString
{
private:
	char *dataPtr;
	int size;
public:

	//Constructor and Destructor
	MyString(); //Default Constructor
	MyString(MyString &); //Copy Constructor
	~MyString(); //Destructor

	//Functions
	void input();
	void print();
	void clearPrint();

	//Operator Overloading
	MyString &operator+(MyString);
	MyString &operator=(MyString &);
	bool operator==(MyString);
};

#endif 


MyString.cpp - MyString class definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//MyString class definition

#include "stdafx.h"
#include "iostream"
using std::cout;
using std::cin;
using std::endl;

#include "string.h"
using std::strcpy;
using std::strlen;

#include "MyString.h"

MyString::MyString() //Default Constructor
	:size(0),
	dataPtr(new char[100])
{}

MyString::MyString(MyString &copy) //Copy Constructor
{
	size=copy.size;
	dataPtr=new char[size+1];
	strcpy(dataPtr,copy.dataPtr);
}

MyString::~MyString() //Destructor
{
	delete []dataPtr;
}

void MyString::input()
{
	cout<<endl;
	cout<<"Enter your string (100 chars max): ";
	cin.getline(dataPtr,100);
	size=strlen(dataPtr);
}

void MyString::print()
{
	cout<<endl<<"Your string: "<<dataPtr;
}

void MyString::clearPrint()
{
	cout<<endl<<dataPtr;
}

MyString &MyString::operator+(MyString right)
{
	MyString temp; //For temporarily store string
	delete []temp.dataPtr; //making space for new string coming up
	temp.size=this->size+right.size; //prepare size for temp string
	temp.dataPtr=new char[temp.size+1]; //Allocate space for temp string

	strcpy(temp.dataPtr,this->dataPtr);
	strcat(temp.dataPtr,right.dataPtr);

	//Enable cascading
	delete []this->dataPtr;
	this->dataPtr=new char[temp.size+1];
	strcpy(this->dataPtr,temp.dataPtr);
	return *this;
}

MyString &MyString::operator=(MyString &right)
{
	if(this!=&right) //avoid self assign
	{
		delete []this->dataPtr;
		this->dataPtr=new char[right.size+1];
		size=right.size+1;
		strcpy(this->dataPtr,right.dataPtr);
		return *this;
	}
	else
	{
		cout<<"Error: Self Assign";
		return *this;
	}
}

bool MyString::operator==(MyString right)
{
	return strcmp(this->dataPtr,right.dataPtr) == 0;
}


Main Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Main Program

#include "stdafx.h"
#include "iostream"
using std::cout;
using std::cin;
using std::endl;

#include "MyString.h"

int main()
{
	MyString a;
	a.input();
	a.print();

	MyString b(a);
	b.print();

	cout<<"a == b? : ";
	if(a==b)
		cout<<"True";
	else
		cout<<"False";

	cout<<endl;
	cout<<"a + b: ";
	MyString c;
	c=a+b;
	c.print();

	cout<<endl;
	return 0;
}


And here's the error message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!

Program: ...\OOP\OPP Homeworks\Projects\2\6-MyString\Debug\6-MyString.exe

HEAP CORRUPTION DETECTED: after Normal block (#132) at 0x003456C0.
CRT detected that the application wrote to memory after end of heap buffer.


(Press Retry to debug the application)
---------------------------
Abort   Retry   Ignore   
---------------------------


What's wrong with my code? I tried to run step by step. Nothing wrong when debugging, but at the end (line "return 0;" in main()), the error show up.
This part makes no sense to me

1
2
3
4
//Enable cascading
delete []this->dataPtr;
this->dataPtr=new char[temp.size+1];
strcpy(this->dataPtr,temp.dataPtr);
I want to use "+" with many MyString
for example
1
2
3
4
5
MyString a,b,c,d;
a.input();
b.input();
c.input();
d=a+b+c;


in the operator overloading +, it must
 
return *this;

to enable this ability
I still don't understand what that code is trying to do. Removing it fixes the error.
Topic archived. No new replies allowed.