Sign in to follow this  
gandolf1212

Bug with my compiler?

Recommended Posts

gandolf1212    100
This does not compile, commentedOutLine and currentLine are both strings. Shouldn't this declare commentedOutLine, assign it "#", and then call the += string operator to add current line to it? Simple to get around, just wondered if there was a reason this didn't work or if I found a compiler bug. (string commentedOutLine = "#") += currentLine;

Share this post


Link to post
Share on other sites
echohead    174
string commentedOutLine("#" + currentLine);

I think that the reason it doesn't work is because the value of (string commentedOutLine = "#") is the character constant "#", and not 'string commentedOutLine'. and you can't assign current line to the character constant.

Share this post


Link to post
Share on other sites
Excors    715
There is a difference between initialisation - like
string currentLine = "test";
- and assignment, like
currentLine = "test";
Only the latter can be used as an expression, i.e. as part of a statement like
something += currentLine;

So you can say
string commentedOutLine;
(commentedOutLine = "#") += currentLine;
but you can't use your original code because it's not valid C++. Or, more conventionally, don't use += and instead say
string commentedOutLine = "#" + currentLine;

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by bballmitch
if you are using C++, you should use:

char myString[numberOfChars] = "My String";


NO !


gandolf1212 - a variable definition is a statement, not an expression.
string commentedOutLine = "#";
commentedOutLine += currentLine;

Share this post


Link to post
Share on other sites
Deranged    668
Quote:
Original post by Fruny
Quote:
Original post by bballmitch
if you are using C++, you should use:

char myString[numberOfChars] = "My String";


NO !


Quoted for emphasis,

If you use C++, it is generally a bad idea to use a char array when so much better alternatives are available.

Edit: especially since he would have to use things like sprintf and such which can yield unexpected results in certain cases. Avoid C strings and <string.h> like the plague if you use C++.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this