| | |
Trying to overload + for class to add to string 'is illegal'
![]() |
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;
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;
C Syntax (Toggle Plain Text)
string operator+ ( const string& s, const Dan& d ) { return s + d.toString(); }
C Syntax (Toggle Plain Text)
string operator+ ( const Dan& d, const string& s ) { return s + d.toString(); }
C Syntax (Toggle Plain Text)
string toString() const { return first + ", " + last; }
I'm here to prove you wrong.
>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:
>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.
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:
C Syntax (Toggle Plain Text)
class Dan { // Data members public: string toString() const; // No operator+ here }; string operator+ ( const string& s, const Dan& d ) { return s + d.toString(); } string operator+ ( const Dan& d, const string& s ) { return s + d.toString(); }
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.
I'm here to prove you wrong.
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.
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.
>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:
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.
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:
C Syntax (Toggle Plain Text)
string operator+ ( const Dan& d, const string& s ) { return d.toString() + s; }
>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.
I'm here to prove you wrong.
![]() |
Similar Threads
- Programming Ideas (C++)
- need help in creating class string (C++)
- "String class" (Java)
- Class Passing and Returning Arrays (Java)
Other Threads in the C Forum
- Previous Thread: Can somebody explain to me about the 'strcmp'
- Next Thread: Microsoft YCMD Software Giveaway!
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






