# 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 on other sites
bballmitch    100
if you are using C++, you should use:

char myString[numberOfChars] = "My String";

##### Share on other sites
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 on other sites
Ezbez    1164
Or use std::string, which is definately the way to go.

##### 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 on other sites
Fruny    1658
Quote:
 Original post by bballmitchif 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 on other sites
Deranged    668
Quote:
Original post by Fruny
Quote:
 Original post by bballmitchif 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++.