Incompatible types issue

hi there, I have a lib function that returns one word of type const char*. I want to compare this word in an if statement condition against a C++ String word. When I do this the system complains about Incompatible types and undefined behavior. How can I do this?
Two ways to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>
using namespace std;

int main ()
{   const char * str1 = "str1";
    const string str2 = "str2";

    //	Make sure the C++ string is on the left
    if (str2 == str1)
        cout << "Strings are the same" << endl;

    //	C method
    if (strcmp (str1,str2.c_str()) == 0)
        cout << "Strings are the same" << endl;
    return 0;
}


edit: removed comment per Peter87's post.
Last edited on
Make sure the C++ string is on the left

No need. The == operator has been overloaded to handle any order.
http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp
Topic archived. No new replies allowed.