Skip to main content
GameDev.net gamedev.net
🔒 Locked

Overloading Operator Problem

Started by utwo007 Apr 10, 2002 at 3:52 PM 7 replies 150+ views
Original Post
utwo007
utwo007
Got a little problem. I''m creating a String class, and I''m getting a weird compiler error. Here''s what I have so far. The problem is with overloading the ''+'' operator. I want to be able to do something like this:
string3 = string1 + string2;
And I figured that the best way to do it would be to create a temporary string object, set that object to the union of string1 and string2, and then return that temporary object. It would look like this. I wont put the whole code here, but rather just the general logic of it so you get the gist:
string operator + (string &string2)
{

 string temp;
 
 // Set temp.length equal to the added lengths of both strings involved.

 // Use strcopy''s to copy data from both strings into temp''s buffer.

 return temp;

} 
So, the above expression would essentialy end up like this:
string3 = temp;
And then I''ve overloaded the ''='' operator to do what''s expected. The problem is that I am getting this error that says "cannot return temporary" from the Bloodshed''s IDE (I forget which compiler it uses -- starts with M). Why the hell can''t I? This seems like the simplest, most logical thing to do. I''ve persued other methods, such as using a global temporary string object, as well as altering the calling object (in this case string1) so that it equals the result of string1 + string2. Nothing works. I even tried having the overloaded ''+'' function return a char*, and I overloaded ''='' to accept a char*, and I got the same dang message. Anyone know what I''m doing wrong? ---signature--- People get ready. I''m ready to play.
---signature---People get ready.I'm ready to play.
SabreMan
SabreMan
Are you sure the compiler is complaining about the piece of code you showed? You might want to try posting the smallest code sample you can which recreates the error. The code you have showed shouldn''t cause the diagnostic you have described.
krez
krez
i believe the temp variable goes out of scope when the function exits, so you are returning something that is (might be?) immediately destroyed.

EDIT: sorry, my bad. evercrack has turned my brains to mush

[edited by - krez on April 10, 2002 7:45:49 PM]
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Oluseyi
Oluseyi
quote:
Original post by krez
i believe the temp variable goes out of scope when the function exits, so you are returning something that is (might be?) immediately destroyed.

No. He returns by value, which means the temp value is copied to the receiving variable. Follow SabreMan''s instructions.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
utwo007
utwo007
ok here is the code and the compiler error:

// In the main cpp driver the call

s3 = s2 + s1; // where s1 s2 and s3 are all of user defined
// type String


And here's the overloaded operator function:


const String &String::operator+( const String &right )
{

String Temp;

Temp.sptr = new char[length + right.length+1];
strcpy(Temp.sptr, sptr);
strcat(Temp.sptr, right.sptr);
return Temp;

}

//so s1 + s2 should essentially be replaced by Temp, which would then read s3 = Temp


So then this function is called:



[code]
const String &String::operator=( const String &right ){

cout << "Operator = called ";

if ( &right != this )
{
delete [] sptr;
length=right.length;
setString( right.sptr );

}

else
cout << "Attempted to assign a string to itself";
return *this; //enables cascading assignment

//the setstring function is this

void String::setString(const char *string2 ){

sptr=new char [ length +1 ];
assert( sptr != 0 );
strcpy( sptr, string2);
}



the warning given is
97 functions5.cpp
warning: reference to local variable `Temp' returned


Anyone have any ideas?

EDIT: Added some text-formatting.



[edited by - utwo007 on April 10, 2002 9:14:12 PM]
---signature---People get ready.I'm ready to play.
Dragonskin
Dragonskin
Remove the "&" in the return type of the addition operator. That's what's giving you the problem (so it doesn't make the copy, it's going out of scope as krez mentioned).

[edited by - dragonskin on April 10, 2002 9:28:19 PM]
mmelson
mmelson
const String &String::operator+( const String &right )

should be

const String String::operator+( const String &right )

you were trying to return a reference to a local var, and that''s a no-no.
utwo007
utwo007
Crap, that causes another problem but ill work on that now. Thanks for the help mates!
---signature---People get ready.I'm ready to play.
SabreMan
SabreMan
quote:
Original post by mmelson
should be

const String String::operator+( const String &right )

Not quite. By a strange quirk of C++, the return value should be non-const value. This allows you to do form degenerate structures like:

(s1 + s2) = s3;

Completely stupid, but its what the built-in types do, and its what the Standard Library is allowed to expect of your UDTs.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.