0

I am referring to this post on how to create a string from a char in cpp.

Convert a single character to a string?

c is a charactor and inputstr is a vector of strings. As stated, if I do this

string str2(1,c);
inputstr.push_back(str2);

it works and inputstr gets a str append to the end but not when I do

inputstr.push_back(string str2(1,c));

It throws an error: expected primary-expression before ‘strx’ inputstr.push_back(string strx(1,c)); What does the error mean? is it because cpp does not support in line declaration, I would expect a different error in that case.

2
  • 1
    You want inputstr.push_back(string(1,c));. Commented Oct 27, 2017 at 1:22
  • 1
    The error means that this is not valid C++. Commented Oct 27, 2017 at 1:22

1 Answer 1

2

But,

inputstr.push_back(string(1, c));

should work.

In your example, you are attempting to declare a variable in an invalid place and that is not valid code, so the compilation fails. In this example, you are simply instantiating a string object and passing the instance to push_back(). You don't need the variable in this case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.