Take a closer look at the error:
if(isPalindrome(s)){
palindrome.cpp:4:5: note: candidate function not viable: no known conversion from 'std::__1::string' (aka 'basic_string<char,
char_traits, allocator >') to 'char *' for 1st argument
So let's break it down:
1- if(isPalindrome(s)){
That's the line that failed compiling. The line has an if
statement, a call to the isPalindrome
function, and a reference to variable s
. It also has some C syntax related to those, ( ( )){
. The error could be in any of those. You do have an arrow to the isPalindrome
function, so that's our best bet, but not necessarily the root cause yet.
2- palindrome.cpp:4:5:
That's an offending line (or at least, a line related to your issue). What this line does,
int isPalindrome(char* s){
is just declare a function. So now we know the issue is at least related to your function declaration, confirming the suspicion above.
2- candidate function not viable:
this goes a bit more detail into what the problem with that function is.
Each function call has to be matched to a function declaration. C++ accepts (function overloading)[https://en.wikipedia.org/wiki/Function_overloading], so given a function call, the compiler will find all "candidate" functions, that is, those that have a matching name. Between those, the compiler will look at certain hints, such as the types you're passing in, to determine which implementation to use.
The error here declares that there is no viable function. So there was no function that could be matched to the call you're making.
3- *no known conversion from 'std::__1::string' [...] to 'char ' for 1st argument:
Now this is the remaining piece we need. The compiler tells you that in the best candidate function it found, for the first (and only) argument, there is a type mismatch. The function expects a char *
, but the argument being used in your call can't be converted to a char *
. The argument you're passing is of type std::string
, and that can't be converted to a char *
automatically.
There are several ways to fix this, but the easiest route is to change your types so that either the variable you pass in is a char *
, or the function you're defining expects the type you're passing in, that is, std::string
.
As someone mentioned above, one way to achieve this, among several others, is to just add s.c_str()
to your function call, instead of purely passing in s
. That is a manual conversion from std::string
to char *
.
In general though, converting types with manual calls isn't a good solution from a performance or maintainability standpoint. You'd be better off just reimplementing either your function or your input reading to use the same type throughout, be it std::string
or char *
.