First time posting here. I have gained a lot from this website.
I need a little help with a homework assignment. I have created a String class to handle basic string operations (Copy, Concat, Compare, Display, & Length). We are adding overloaded operators to the class and in particlular I am having trouble with the < operator. What I have done thus far works with a string on the lhs and a string on the rhs. It also works with a string object on the lhs and a string object on the rhs. Where I am having trouble is if I have a string on the lhs and a string object on the rhs. I get the error:
error C2679: binary '<' : no operator found which takes a right-hand operand of type 'String' (or there is no acceptable conversion)
1> could be 'built-in C++ operator<(const char [5], const char [5])'
1> while trying to match the argument list '(const char [5], String)'
Can someone please clue me in to what I am missing? The problem is refering to two lines in main.cpp near the end of the program. Thanks in advance.
As your compiler says, you haven't provided an overloaded < operator suitable for this case.
This -> booloperator < (constchar []); won't work, because the order of the arguments doesn't match.
This -> booloperator < (const String &); won't work, because of this -> explicit String (constchar []);
Since you don't allow implicit casts, your compiler isn't allowed to turn "efgh" or "abcd" into Strings and then call the appropriate overload.either
A possible solution is to also provide something like this (not as a member function):
I understood the problem, but not the solution. I discussed this with my instructor as well and his solution is exactly the same as yours. The final complete solution was
inline bool operator < (const char C [], const String & S)
{
return (S > C);
}
Of course the I had to define the > operator to make it all work.