Compare Strings

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)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  // in main
 MyString s1, s2("hello"), s3("world");

cout << "Is s2 less than s3 " << s2 < s3;

}



namespace cs_mystring
{
	class MyString
	{
	public:
		MyString();
		MyString(const char* inString);
		friend std::ostream& operator << (std::ostream& out, const MyString& right);
		friend bool operator < (const MyString& left, const MyString& right);

	private:
		char *ptr;
	};
}




bool operator < (const MyString& left, const MyString& right)
	{
		int compare = strcmp(left.ptr, right.ptr);

		return  (compare == 0);
	}
Last edited on
<< has higher precedence than < so you need to use parentheses.

 
cout << "Is s2 less than s3 " << (s2 < s3);
Thank you for pointing that out.

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?
I think I see it. I changed the == to < inside the function.
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?


}




bool operator < (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);
			
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstring>
#include <iomanip>

namespace cs_mystring
{
	class MyString
	{
        public:
            MyString() : ptr("") {}
            MyString( const char* inString ) : ptr(inString) {}

            friend std::ostream& operator << ( std::ostream& out, MyString right )
            { return out << std::quoted(right.ptr) ; }

            friend bool operator < ( MyString left, MyString right )
            { return std::strcmp( left.ptr, right.ptr ) < 0 ; }

        private:
            const char* ptr;
	};
}

int main()
{
    const cs_mystring::MyString strings[] = { "", "hello", "hell", "Hello" };
    
     std::cout << std::boolalpha ;
     
    for( const auto& a : strings ) for( const auto& b : strings )
	    std::cout << a << " < " << b << " ? " << (a<b) << '\n' ;
}

http://coliru.stacked-crooked.com/a/39a7c87074a2cbb1
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.
> 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?

Check your code again; strcmp will return 0 when two equal strings are compared.
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.

Topic archived. No new replies allowed.