Jump to content
  • Advertisement
Sign in to follow this  

Bug with my compiler?

This topic is 4383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!