I've been trying to define my own string class and have barely gotten started but am already getting an obnoxious error :/. I'm getting a "Debug Error: Heap Corruption Detected: After Normal Block(#61) CRT detected that application wrote to memory after end of heap buffer." I've had exceptions like this before, but they're just when I've accidently screwed with a pointer after deleting its memory. Here's my class definition so far:
I'm encountering some more troubles overloading the += operator. I haven't even tried the + operator yet. The += operator keeps making the damn program crash! Here's the class definition now:
Also in your operator=(), are you want to delete the memory at str on line 33? I would think you wouldn't want to do anything if the objects are in fact the same.
In your operator+() you don't ever assign the contents of this->str to temp.
@firedraco, yeah I realized that and am working on it with limited success :-(
@Zhuge, the first thing was a typo, thank you. I haven't really even got started with the operator+ yet, so I have some work to do!
@kfmfe04, I just did that in the beginning to test my copy constructor
@fun2code, very good point!
More heap corruption errors with the += operator! I'm trying to solve these myself, but you guys are all far better programmers than me :o. Sorry if I'm coming off as a help vampire :/ I'll try to fix this when I have a chance next:
Line 54, you allocate some memory. You then immediately lose the pointer to it (creating a memory leak) on line 55. Why are you even creating "temp"? See next item.
Also, from line 57 and 59, you are returning a reference to a local variable, which is invalid. You should make the changes to this->str in-place (you are supposed to be modifying the variable itself, anyway), instead, and return *this.
The problem is that I can't make the changes to this->str immediately because it is too small to hold both of the strings. That's why I created temp. Temp would be larrge enough to hold both of the strings. So I tried to copy the value of this->str to temp, which I obviously screwed up, leading to that memory leak. Since I'll now be returning *this[/code[, I guess to concatenate [code]string.str with temp, and then make this->str = temp should . Obviously this doesn't work though :/ I'll keep working on it.