Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting character literals to strings

16 views
Skip to first unread message

Paul

unread,
Jun 13, 2015, 1:35:23 PM6/13/15
to
A C++ textbook says "The string library lets us convert both character literals and character string literals to strings"

I don't think I'm included in this "us" because std::string s = 's'; isn't accepted by my compiler. Is this yet another textbook error?

Thank You,

Paul
Message has been deleted

Barry Schwarz

unread,
Jun 13, 2015, 2:08:06 PM6/13/15
to
On Sat, 13 Jun 2015 10:35:13 -0700 (PDT), Paul <peps...@gmail.com>
wrote:

>A C++ textbook says "The string library lets us convert both character literals and character string literals to strings"
>
>I don't think I'm included in this "us" because std::string s = 's'; isn't accepted by my compiler. Is this yet another textbook error?

You are trying to do it as initialization. To do that, you need to use
the "fill" form of the constructor since there is no other char form
(see www.cplusplus.com/reference/string/string/string/):
std::string s (1,'s');

The operator= has a char form. The conversion works for assignment,
as in:
std::string s;
s = 'x';

--
Remove del for email
0 new messages