Click to See Complete Forum and Search --> : C++ problem
Stuka
12-06-2000, 02:35 PM
Hey, I've got a strange (to me!) problem. The following code works inside a regular member function of a class, but fails when used in an overloaded += operator. Any clue as to why it would do this?
char* MMCDataBuffer::Add(char add_char)
{
char* temp_buf = buffer;
buffer = new char[size + 1];
for (int i = 0; i < size; ++i)
{
buffer[i] = temp_buf[i];
}
delete [] temp_buf;
buffer[size] = add_char;
size += 1;
return buffer;
}
I just can't understand why it works one place, and fails another (literally the SAME code!)
BrianDrozd
12-06-2000, 02:46 PM
First and foremost, you're encountering this problem because you have the function prototype wrong. I'm not quite possitive (i.e. find a reference somewhere and look it up before taking my word as gospel) but I beleive the funtion prototype for += should be something like:
MMCDataBuffer MMCDataBuffer::operator +=(char add_char)
As such, for += and other similar functions the return value should be something like: return this;
[This message has been edited by BrianDrozd (edited 06 December 2000).]
Stuka
12-06-2000, 03:02 PM
Brian- sorry I wasn't clear. That is the function that works. I'll check that prototype out - I'm away from my references and I haven't done much operator overloading. Thanks, and I'll yell if it don't work! http://www.linuxnewbie.org/ubb/smile.gif
Stuka
12-07-2000, 02:10 AM
OK for anyone who's concerned - I had the prototype set up right on the operator +=. What the compiler is barfing on is my delete[] temp_buf. Now, I know I need to clear that memory, but the compiler is telling me that I can't, since it wasn't created on the local heap. How can this be??? The memory is allocated by the ctor, and the delete[] works fine in the member function!
BrianDrozd
12-07-2000, 09:41 AM
Originally posted by Stuka:
OK for anyone who's concerned - I had the prototype set up right on the operator +=. What the compiler is barfing on is my delete[] temp_buf. Now, I know I need to clear that memory, but the compiler is telling me that I can't, since it wasn't created on the local heap. How can this be??? The memory is allocated by the ctor, and the delete[] works fine in the member function!
Ah ha! That is vital info.
Now, I could only guess at why there is a problem. Have you tried setting setting the appropriate flags to force ASNI compatability?
I do have a suggestion on how to fix it, however, should all else fail.
Try:
MMCDataBuffer MMCDataBuffer::operator +=(char add_char)
{
this.add (add_char);
return this;
}
[/B][/QUOTE]
I know, it seems kind of silly, but this should get you around the problem. Besides, this would be the prefered method, provided you plan on keeping the add function. Better code reuse, after all.
Stuka
12-07-2000, 10:33 AM
Ironically enough, I tried just that. Well, almost - I still think the return type on the += operator needs to be char* for this - in fact when I used a prototype identical to what you suggested, it complained about converting the return type. I also didn't use the this pointer (BTW, this IS a pointer! http://www.linuxnewbie.org/ubb/smile.gif), but still got the same error. Well, I brought me a reference text today, so I'm gonna check it out. If ya have any more suggestions, PLEASE let me know! http://www.linuxnewbie.org/ubb/confused.gif
TetsuoII
12-07-2000, 11:22 AM
Could you please post the class members too, it's a bit difficult to get all of the code without seing the rest of the class definition.
T.
Stuka
12-07-2000, 07:05 PM
OK, I solved the problem. Of course, I really just sidestepped it. Pulled out my handy reference book and found an answer. My basic problem was that the string class was inconvenient for what I needed - a formatted buffer of ASCII characters w/o a null terminator. So what I ended up doing was using the ostrtream class. I do appreciate the help though, and if anyone is interested, I can post the code for all to see.