Operator Overloading Help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Operator Overloading Help

 
0
  #1
Jul 12th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,508
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Operator Overloading Help

 
0
  #2
Jul 12th, 2009
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Re: Operator Overloading Help

 
0
  #3
Jul 12th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,414
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Operator Overloading Help

 
0
  #4
Jul 12th, 2009
Originally Posted by lancevo3 View Post
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?
  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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Re: Operator Overloading Help

 
0
  #5
Jul 12th, 2009
I went ahead and applied the changes but I left const on the end because of the requirement would that throw things off?

  1.  
  2. MyString MyString::operator+(const MyString& rightOp) const
  3. {
  4. MyString tmp = *this;
  5. strcpy(tmp.stringStorage, rightOp.stringStorage);
  6. return tmp;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,508
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Operator Overloading Help

 
0
  #6
Jul 12th, 2009
Yup -- I tested that too and it compiled ok.
Last edited by Ancient Dragon; Jul 12th, 2009 at 3:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,508
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Operator Overloading Help

 
0
  #7
Jul 12th, 2009
Originally Posted by Dave Sinkula View Post
Don't you want this function to be outside of the class?
  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.
error C2270: '+' : modifiers not allowed on nonmember functions
Last edited by Ancient Dragon; Jul 12th, 2009 at 3:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Re: Operator Overloading Help

 
0
  #8
Jul 12th, 2009
Alright well have another question about a different + overloading because I have to do three and they are giving issues.

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,508
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Operator Overloading Help

 
0
  #9
Jul 12th, 2009
Look at the function I posted above. All you have to do is change the parameter types in the three overloads
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,706
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Operator Overloading Help

 
1
  #10
Jul 13th, 2009
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC