Non-standard compiler??

Dec 3, 2012 at 9:26pm
I am working out of a book that claims that their examples comply to standard C++. There is a particular line of code in an example in their book that is generating errors for my compiler:

1
2
3
4
if ((char ch = inBuf[ix]) == '.')
{
    /*various code here*/
}


It generates the error:
error C2146: syntax error : missing ')' before identifier 'ch'

The book does say that using 'ch' here in this case is unnecessary but it was used to illustrate a point.

I am using Visual C++ 6.0.
Any insight would be appreciated, thanks.
Dec 3, 2012 at 9:53pm
Not sure if it's because VC6 is too old or not. Have you tried with another compiler? GCC is up to 4.7.2 and is pretty current with C++11 standard.
Dec 3, 2012 at 10:07pm
which book is that, because I believe that statement is illegal in C++.
Dec 3, 2012 at 11:14pm
This is not allowed. You will have to declare ch outside the if statement.
1
2
3
4
5
char ch;
if ((ch = inBuf[ix]) == '.')
{
    /*various code here*/
}
Dec 4, 2012 at 12:19am
Thanks for the responses, the book is C++ primer 3rd edition.
Dec 4, 2012 at 1:09am
Third edition is from early 1998, when the first revision of the standard was just on its way to publication. Even the coauthors of C++ can make mistakes. Get the fifth edition, from 2012.
Topic archived. No new replies allowed.