How to pass a char to a function expecting a string

Neil Kirk

I want to pass a char to a parameter expecting a string.

void test(const string&);

test('a'); // does not like

error: invalid user-defined conversion from ‘char’ to ‘const string& {aka const std::basic_string<char>&}’

I know I can change ' to ", but in my real code it's not a literal at that point.

How can I conveniently get this to compile?

Mike Seymour

There's no implicit conversion from a character to a string. You'll have to make a string using the appropriate constructor, which has another parameter to specify the length:

test(std::string(1, 'a'));

or, since C++11, with an initialiser list

test({'a'});             // if there are no ambiguous overloads of "test"
test(std::string{'a'});  // if you need to specify the type

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass char into function?

From Dev

How do I pass a list of values to a function expecting an array?

From Dev

How to use Array[String] in a function expecting String* in Scala

From Dev

How to Pass a String in a function?

From Dev

Pass function (delegate?) to function expecting object as parameter

From Dev

How to pass an array of char's to a function in C

From Dev

How to pass in function a multidimensional char array in C?

From Dev

How to pass in function a multidimensional char array in C?

From Dev

how to pass 2D array type char (string) as function parameter?

From Dev

How to pass String as GLchar** (char**) parameter in glShaderSource?

From Dev

Pass integer address in method expecting unsigned char pointer

From Dev

How to pass a string into a function in Python

From Dev

How to pass a Struct to a method that is expecting an AnyObject?

From Dev

Storing array of string literals, to pass to template expecting string literals

From Dev

How to pass an array of Swift strings to a C function taking a char ** parameter

From Dev

How to pass a float array to the function that has unsigned char pointer as an argument?

From Dev

How many ways are there to pass char array to function in C?

From Dev

How to pass const char* from python to c function

From Dev

cstr literal in C++11, How to pass an (char* []) to a function

From Dev

Pass pointer to data as argument to function expecting two-dimensional array

From Dev

Pass vector of inherited class object to function expecting base class vector

From Dev

Pass double to a function expecting uint8_t*

From Dev

how to pass a string to a macro sas without special char

From Dev

How do i pass a string using char into fopen C

From Dev

How to fix Expecting 'STRING' error in JSON

From Dev

How to pass a Swift string to a c function?

From Dev

How to pass a string to Angular 2 (click) function?

From Dev

how to pass string in onclick function html

From Dev

How to pass a string as an argument to a javascript function

Related Related

  1. 1

    How to pass char into function?

  2. 2

    How do I pass a list of values to a function expecting an array?

  3. 3

    How to use Array[String] in a function expecting String* in Scala

  4. 4

    How to Pass a String in a function?

  5. 5

    Pass function (delegate?) to function expecting object as parameter

  6. 6

    How to pass an array of char's to a function in C

  7. 7

    How to pass in function a multidimensional char array in C?

  8. 8

    How to pass in function a multidimensional char array in C?

  9. 9

    how to pass 2D array type char (string) as function parameter?

  10. 10

    How to pass String as GLchar** (char**) parameter in glShaderSource?

  11. 11

    Pass integer address in method expecting unsigned char pointer

  12. 12

    How to pass a string into a function in Python

  13. 13

    How to pass a Struct to a method that is expecting an AnyObject?

  14. 14

    Storing array of string literals, to pass to template expecting string literals

  15. 15

    How to pass an array of Swift strings to a C function taking a char ** parameter

  16. 16

    How to pass a float array to the function that has unsigned char pointer as an argument?

  17. 17

    How many ways are there to pass char array to function in C?

  18. 18

    How to pass const char* from python to c function

  19. 19

    cstr literal in C++11, How to pass an (char* []) to a function

  20. 20

    Pass pointer to data as argument to function expecting two-dimensional array

  21. 21

    Pass vector of inherited class object to function expecting base class vector

  22. 22

    Pass double to a function expecting uint8_t*

  23. 23

    how to pass a string to a macro sas without special char

  24. 24

    How do i pass a string using char into fopen C

  25. 25

    How to fix Expecting 'STRING' error in JSON

  26. 26

    How to pass a Swift string to a c function?

  27. 27

    How to pass a string to Angular 2 (click) function?

  28. 28

    how to pass string in onclick function html

  29. 29

    How to pass a string as an argument to a javascript function

HotTag

Archive