简体   繁体   中英

In constructor ‘Player::Player()’: error: invalid conversion from ‘const char*’ to ‘char’

I have this line in my Blackjack default constructor.

m_players[0].SetPlayerName("Jane");

This uses a setter in my Player class to set the player's name as Jane.

My Player constructor is this.

Player::Player()
{
  Player player;
  player.m_funds = 0;
  player.m_name = "";
  player.m_bet = 0;
  player.m_busted = false;
}

And, for more information, my SetPlayerName method is this.

void Player::SetPlayerName(char name)
{
  m_name = name;
}

How do I fix this conversion error? I am just trying to set the player's name to be a char. Thanks!

I am just trying to setup a default constructor with one player, Jane.

A String literal (something like "Foobar" is of type const char[N] where N is the amount of characters + 1 (for the null terminator), meaning array of N char .

Your variable m_name seems to be of type char which is a single char.

There is no way to convert an array of const char[N] to char . What you really want is m_name to be of type std::string or possibly const char* .

Why const char* ? A const char[N] decays to a const char* and can point to a literal of any size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM