943,863 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 793
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 12th, 2009
0

Operator Overloading Help

Expand Post »
Been working with operator overloading for the first time and am running into quite the standstill with this one. I finally got rid of the code I had written and decided to start from scratch. If anyone has any advice on how to code this for me it be a great help for me to see an example on how this would work.

This is what I need to do for it.

MyString MyString::operator+(const MyString& rightOp) const

The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.


Here is what I originally had if this helps anyone solve my issue.

C++ Syntax (Toggle Plain Text)
  1. MyString MyString::operator+(const MyString& rightOp) const
  2. {
  3. strcat(this->stringStorage, rightOp.stringStorage);
  4. return *this;
  5. }
Last edited by lancevo3; Jul 12th, 2009 at 3:02 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Jul 12th, 2009
0

Re: Operator Overloading Help

I see two things wrong:
1) remove the const keyword at the end -- the function is not const because it changes the value of stringStorage.

2)You need another class object. What you wrote was for the += operator, not the + operator.
C++ Syntax (Toggle Plain Text)
  1. MyString MyString::operator+(const MyString& rightOp)
  2. {
  3. MyString tmp = *this; // requires overloaded = operator
  4. strcpy(tmp.stringStorage, rightOp.stringStorage);
  5. return tmp;
  6. }
Last edited by Ancient Dragon; Jul 12th, 2009 at 3:21 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 12th, 2009
0

Re: Operator Overloading Help

I don't have a choice on the const keyword on the end because its part of the assignment. Should I be returning rightOp then?
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Jul 12th, 2009
0

Re: Operator Overloading Help

Click to Expand / Collapse  Quote originally posted by lancevo3 ...
The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.
Don't you want this function to be outside of the class?
c++ Syntax (Toggle Plain Text)
  1. class MyString
  2. {
  3. // ...
  4. };
  5.  
  6. MyString operator+ (const MyString &lhs, const MyString &rhs)
  7. {
  8. return MyString(lhs) + MyString(rhs);
  9. }
Or something like that?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 12th, 2009
0

Re: Operator Overloading Help

I went ahead and applied the changes but I left const on the end because of the requirement would that throw things off?

C++ Syntax (Toggle Plain Text)
  1.  
  2. MyString MyString::operator+(const MyString& rightOp) const
  3. {
  4. MyString tmp = *this;
  5. strcpy(tmp.stringStorage, rightOp.stringStorage);
  6. return tmp;
  7. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Jul 12th, 2009
0

Re: Operator Overloading Help

Yup -- I tested that too and it compiled ok.
Last edited by Ancient Dragon; Jul 12th, 2009 at 3:35 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 12th, 2009
0

Re: Operator Overloading Help

Don't you want this function to be outside of the class?
c++ Syntax (Toggle Plain Text)
  1. class MyString
  2. {
  3. // ...
  4. };
  5.  
  6. MyString operator+ (const MyString &lhs, const MyString &rhs)
  7. {
  8. return MyString(lhs) + MyString(rhs);
  9. }
Or something like that?
No -- that is illegal syntax for non-class methods.
Quote ...
error C2270: '+' : modifiers not allowed on nonmember functions
Last edited by Ancient Dragon; Jul 12th, 2009 at 3:39 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 12th, 2009
0

Re: Operator Overloading Help

Alright well have another question about a different + overloading because I have to do three and they are giving issues.

C++ Syntax (Toggle Plain Text)
  1. MyString MyString::operator+(const char* rightOp) const
  2. {
  3. strcat(this->stringStorage, rightOp);
  4. return *this;
  5. }

#

MyString MyString::operator+(const char* rightOp) const

The addition operator should be overloaded to take a MyString object and a C-style string, concatenate their text together, and return a new MyString object that contains the result.

Anymore advice would be great. I have to write three + overloads and they are just giving me an issue where the other ones I did not have as much problem.
Last edited by lancevo3; Jul 12th, 2009 at 3:40 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Jul 12th, 2009
0

Re: Operator Overloading Help

Look at the function I posted above. All you have to do is change the parameter types in the three overloads
C++ Syntax (Toggle Plain Text)
  1. MyString MyString::operator+(const MyString& rightOp) const
  2. MyString MyString::operator+(const char* rightOp) const
  3. MyString MyString::operator+(const std::string& rightOp) const
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jul 13th, 2009
1

Re: Operator Overloading Help

char *strcat( char *str1, const char *str2 );

That's the prototype for strcat() from the online reference I usually refer to. I always thought str1 needed to be a null terminated char array and str2 needed to be either a null terminated char array or a string literal. I am also almost certain that the return value of char* will be a null terminated char array. From your previous post I thought that one of the requirements was that the MyString member varialbe called stringStorage could not be null terminated. If that restriction still holds, then you will have to be careful regarding using strcat() and other C style string handling functions to meet the assigment requirements.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: OOP
Next Thread in C++ Forum Timeline: Newbie Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC