Jump to content
  • Advertisement
Sign in to follow this  
Kevn

invalid conversion from 'char' to 'const char*'(using strings)

This topic is 2376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I send a single character from one string into a function parameter of type string to be equal to another string.
For example:

/// The function (thisString is of type string)
void setChar(string c)
{
thisString = c;
}

/// Calling the function
string alphabet = "abcdefg";
setChar(alphabet[0]);


When I do this, i get the error Invalid conversion from 'char' to 'const char*'. Also, no I cannot change the types in the function to char.

Share this post


Link to post
Share on other sites
Advertisement
You can also do:

setChar(std::string(1, alphabet[0])); // creates a string of length 1 consisting of alphabet[0] repeated 1 time
setChar(std::string(alphabet, 0, 1)); // creates a string of length 1 from string alphabet starting from the index 0

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!