7
#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]

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
VarunVyas
  • 1,357
  • 6
  • 15
  • 23

3 Answers3

23

Try replacing

 char c = "i";

with

 char c = 'i';
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
12

"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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
11

"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

Mankarse
  • 39,818
  • 11
  • 97
  • 141