Cretu Bogdan
Cretu Bogdan

Reputation: 31

C++ invalid conversion from 'char' to 'const char*' [-fpermissive]|

I have a problem with this code:

void keylist(char key)
{
   //check if the user presses a key
   if(GetAsyncKeyState(key))
   {
      string skey = key;
      buffer.append(skey);
      counter++;

   }
}

Everytime I try to run the program it gives me this error: CodeBlocks Projects\CB32KLG\main.cpp|66|error: invalid conversion from 'char' to 'const char*' [-fpermissive]|

Upvotes: 2

Views: 8912

Answers (2)

Columbo
Columbo

Reputation: 60979

  string skey = key;

There is no viable converting constructor in string that takes just a char.
Initialize it like this:

string skey{key};

Or this:

string skey(1, key);

Upvotes: 3

ravi
ravi

Reputation: 10733

It's because of this

string skey = key;

There's no overloaded constructor in string which only takes character as its input.See below the complete list:-

string();
string (const string& str); 
string (const string& str, size_t pos, size_t len = npos);  
string (const char* s); 
string (const char* s, size_t n);   
string (size_t n, char c);  
template <class InputIterator>
  string  (InputIterator first, InputIterator last);

To fix you can use:-

string skey(1, key);

Upvotes: 6

Related Questions