4
#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,303
  • 19
  • 89
  • 148
VarunVyas
  • 1,197
  • 5
  • 14
  • 23

3 Answers3

22

Try replacing

 char c = "i";

with

 char c = 'i';
mathematician1975
  • 20,781
  • 6
  • 55
  • 98
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
  • 269,602
  • 39
  • 394
  • 697
paxdiablo
  • 814,905
  • 225
  • 1,535
  • 1,899
10

"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
  • 38,538
  • 10
  • 94
  • 140