6
#include<stdio.h> #include<iostream> #include<fstream> #include<string.h> using namespace std; class base { public: int lookup(char c); }; // class base int base::lookup(char c) { cout << c << endl; } // base::lookup int main() { base b; char c = "i"; b.lookup(c); } // main 

On Compiling above code I am getting below error :

g++ -c test.cpp test.cpp: In function ‘int main()’: test.cpp:20:10: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]

0

3 Answers 3

25

Try replacing

 char c = "i"; 

with

 char c = 'i'; 
Sign up to request clarification or add additional context in comments.

2 Comments

@VarunVyas what error message do you get when you change that line?
If you're using up to two characters like ab or more, then it's rather a string not char @VarunVyas
13

"i" is not a character, it's a character array that basically decays to a pointer to the first element.

You almost certainly want 'i'.

Alternatively, you may actually want a lookup based on more than a single character, in which case you should be using "i" but the type in that case is const char * rather than just char, both when defining c and in the base::lookup() method.

However, if that were the case, I'd give serious thought to using the C++ std::string type rather than const char *. It may not be necessary, but using C++ strings may make your life a lot easier, depending on how much you want to manipulate the actual values.

Comments

12

"i" is a string literal, you probably wanted a char literal: 'i'.

String literals are null terminated arrays of const char (which are implicitly converted to char const* when used in that expression, hence the error).

char literals are just chars

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.