944,098 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2101
  • C RSS
Apr 25th, 2005
0

Trying to overload + for class to add to string 'is illegal'

Expand Post »
Hi,

I'm trying to write code such that my class can be used like so:
To accomplish this,
I'm trying to overload the + operator of my class to return a string. I am able to do this, but when I try and do the operation above, I get:


Error: The operation "std::basic_string<char, std::char_traits<char>, std::allocator<char>> + Dan" is illegal.

What does this mean and how do I make it legal?

class Dan
{
public:
string first;
string last;


const string operator + (Dan & a)
{
return a.toString();
}

string toString()
{
return first + ", " + last;
}

....


Dan temp;
string myString = "Test" + temp;
Similar Threads
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 25th, 2005
0

Re: Trying to overload + for class to add to string 'is illegal'

  1. string operator+ ( const string& s, const Dan& d )
  2. {
  3. return s + d.toString();
  4. }
Since Dan doesn't have a suitable conversion to std::string, you also would need to provide another operator+ with the commutative operation:
  1. string operator+ ( const Dan& d, const string& s )
  2. {
  3. return s + d.toString();
  4. }
toString should also be const since it doesn't modify the state of your object:
  1. string toString() const
  2. {
  3. return first + ", " + last;
  4. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 25th, 2005
0

Re: Trying to overload + for class to add to string 'is illegal'

Narue,

I tried what you posted, but it complains:

Test.cpp", line 13: Error: Illegal number of arguments for Dan::operator+(const std::basic_string<char, std::char_traits<char>, std::allocator<char>>&, const Dan&).
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 25th, 2005
0

Re: Trying to overload + for class to add to string 'is illegal'

>I tried what you posted
No, you didn't. You tried some oddball variation of what I posted after filtering it through your n00b brain. Neither my code nor my comments suggested that operator+ should be a member function. Since toString is a public member function of Dan, neither of the overloads for operator+ should even be friends of Dan. Like this:
  1. class Dan {
  2. // Data members
  3. public:
  4. string toString() const;
  5. // No operator+ here
  6. };
  7.  
  8. string operator+ ( const string& s, const Dan& d )
  9. {
  10. return s + d.toString();
  11. }
  12.  
  13. string operator+ ( const Dan& d, const string& s )
  14. {
  15. return s + d.toString();
  16. }
>but it complains
I'm not surprised. When implemented as a member function, operator+ expects only one argument. When implemented as a non-member function, operator+ expects two arguments. The two are not interchangeable.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 25th, 2005
0

Re: Trying to overload + for class to add to string 'is illegal'

I appreciate the help, (and the insults not so much). In either case, it works now. By the way, I only seemed to need one of the functions, not both. Why do you think I need both?

Also, I changed it to return the first + ", " + last directly since first and last are not const so I couldn't return them using the toString.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 25th, 2005
0

Re: Trying to overload + for class to add to string 'is illegal'

>and the insults not so much
I can live with that.

>Why do you think I need both?
Users of your class will rightfully expect Dan + string to work just as well as string + Dan. Of course, in hindsight I should have written:
  1. string operator+ ( const Dan& d, const string& s )
  2. {
  3. return d.toString() + s;
  4. }
Instead of pasting the body of the other operator+. This way users will get the expected behavior for string concatenation.

>I changed it to return the first + ", " + last directly
Not a good idea. Eventually you'll learn that public data members are a bad thing and make them private, then your code will break. You'll either have to use a public interface for the operators, or make them friends of your class. Both of those entail work on your part, so it's better to do it right from the start.

>since first and last are not const so I couldn't return them using the toString.
That shouldn't matter. A non-const object can be converted to a const object implicitly. The inverse is not true, but that's not the case here. Show me the code that's causing the error, preferably something I can compile directly so I don't accidentally fix something by writing framework code.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: Can somebody explain to me about the 'strcmp'
Next Thread in C Forum Timeline: Microsoft YCMD Software Giveaway!





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


Follow us on Twitter


© 2011 DaniWeb® LLC