Overloaded operands: Comparing two strings

I know you should not compare two strings like this

1
2
 bool operator == (string c)
		{ if ( city == c ) return true ; return false;}


You should use strcmp. I tried using strcmp but
the compiler says that I can't compare a string to a
char (which I am not).

How does overloaded operands (==) work with strings?
Can anyone post an example?

Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main()
{
    std::string a ;
    std::string b ;

    std::cout << "Enter a string: " ;
    std::getline(std::cin, a) ;

    std::cout << "Enter another string: ";
    std::getline(std::cin, b) ;

    if ( a == b )
        std::cout << "You entered the same string twice.\n" ;
    else if ( a < b )
        std::cout <<  '"' << a << "\" comes before \"" << b << "\"\n" ;
    else
        std::cout << '"' << b << "\" comes before \"" << a << "\"\n" ;
}
This page lists the prototypes for, among other things, the equality operation.
http://cplusplus.com/reference/string/string/operators/

You should not use strcmp() with std::strings; just use the == operator to compare them. If city is a std::string member of your class or something, that looks like a legitimate definition to me.
Topic archived. No new replies allowed.