stack <char> stck;
string str;
stck.push('0');
str.append("test:");
//test:
cout << str << endl;
str.append(&stck.top());
//test:0═══════════════¤¤¤¤▌▌▌▌,╘╥XЕ┤
cout << str << endl;
Why is this happening?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Maciej Hehl is correct about why you’re getting unwanted behavior.
To get the behavior that you want, you need to append the character itself, not a pointer to it. You are correct in saying (in your comment to Kalim’s answer) that there is no override of
std::string::appendthat takes just achar. However, there is an overridestd::string::append(std::size_t, char), which appends the character (second argument) a certain number of times (first argument).So the correct way to write what you want would be:
Or, alternatively, just use the overload for the
+=operator, which accepts achar: