I have an overloaded < function that is to compare if two c_string objects are equal or not. I get an error saying no acceptable conversion. Can someone point me in the right direction please.
error C2678: binary '<': no operator found which takes a left-hand operand of type 'std::ostream' (or there is no acceptable conversion)
The results are not making sense to me though. According to my textbook strcmp() returns zero if the strings being compared are equal, < 0 if string one comes before string two, and > 0 if string two comes before string one. My code is printing out zero and the strings are not equal. String one s1 starts with h and s2 starts with w, so h is first going by ascii values. Shouldn't there be a negative value printed out?
Actually, I'm confused. When I cout the value of comparing two equal strings with strcmp() it shows -1. My text book says that strcmp() should return 0 for two strings with equal characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//in main
MyString s1, s2("hello"), s3("hello");
cout << "Is s2 < s3: " << (s2 < s3) << endl; // prints 1. Shouldn't it be 0 for false since hello is not less than hello?
}
booloperator < (const MyString& left, const MyString& right)
{
int compare = strcmp(left.ptr, right.ptr);
cout << "compare is : " << compare << endl; // Prints -1. Shouldn't it print 0 since strcmp return 0 for equal strings?
return (compare < 0);
}
I'm sorry but there is stuff in your code that I am not familiar with yet. Your < function looks similar to mine though. Would you be able to tell me why my strcmp() call is returning -1? Shouldn't it return 0 when two equal strings are compared? I think I need to straighten this out for the rest of the function to flow right.
Your'e right it works. I had one line before that, s2[4] = '!';, to test a different function, and I couldn't see it was messing up my next test until you implied that the code was fine. Kept thinking I had something off with my strcmp call. Thank you.