MSVC 2008 Immediate Window nonsense and some code confusion - c++

I've been fooling with the MSVC 2008 immediate window for the last few hours and I'm flabbergasted with both myself and Microsoft... It probably doesn't help that I stumbled on this mystery at bedtime and it's now 6 hours later. :)
Please see the following:
? "1234567\\87654321\\"
CXX0026: Error: bad format string
I've tried the above several ways in the immediate window and... Nothing. No amount of backslashes gets rid of the error. Removing the backslashes is the only way to solve it.
Does the expression evaluator have something against double backslash in a wide string?
For what it's worth, the immediate window fooling was motivated by the following:
Line 107 is:
size_t endpos = str.find_last_not_of( L”\\/” );
file.cpp(107) : error C2017: illegal escape sequence
file.cpp(107) : error C2017: illegal escape sequence
file.cpp(107) : error C2065: 'L”' : undeclared identifier
file.cpp(107) : error C2065: '”' : undeclared identifier
My questions are:
What's up with the 4 errors on line 107?
What's up with the immediate window? I remember this kind of thing working there a year or so ago. I applied a service pack to MSVC 2008 about 6 months ago but I didn't use it heavily until now.

size_t endpos = str.find_last_not_of( L”\\/” ); // no
size_t endpos = str.find_last_not_of( L"\\/" ); // yes
Beware of code that you copied off a website, maybe a blog post. The author may well have used a word processor, one that implements 'smart quotes'. If you look closely at the first and the second line you'll see the difference. Your compiler will only like the straight double-quotes.
It doesn't quite explain your problem with the Immediate Window, it works when I try your string as shown. Maybe it doesn't quite look like it either.

Related

C++ multiline CString literal in VS2013 Unicode

A multiline CString literal 'str1', accepted without a wink in VS2012 (with MBCS) is now refused at build time, after upgrading to VS2013 (with Unicode, to alleviate tons of errors from the newly deprecated MBCS, even after installing its addon), with the output message:
error C2308: concatenating mismatched strings
as in the following example (A):
str1 = _T(" HELP - available commands \n\n\n"
"F1 : the present help message \n\n");
The first line is reported 'wide' and the second 'narrow'.
I have then tried (B) to add mono-line CString literals:
str1 = _T(" HELP - available commands \n\n\n")
+ _T("F1 : the present help message \n\n");
but the IDE already complains with
Error: expression must have integral or unscoped enum type
and the builder with
error C2110: '+' : cannot add two pointers
It does indeed work if I build (C) the CString str1 with mono-line literals one by one:
str1 = _T(" HELP - available commands \n\n\n");
str1 += _T("F1 : the present help message \n\n");
but I would like to understand why (A) and (B) do not work here, as expected, and as they were so until now. There are several such problems in this (large) program, but in most other similar instances it does work just fine.
Is it due to changes in VS2013 or (and?) the switch from MBCS to Unicode? Are there special characters I overlooked in these strings? And then, how to fix these problems?
Thanks in advance for your responses.
It should be:
str1 = _T(" HELP - available commands \n\n\n") // no semicolon here
_T("F1 : the present help message \n\n");
The reason it worked before is that _T is a no-op for MBCS but for Unicode expands to a width prefix. In C/C++ concatenation is just by making them adjacent with whitespace, but they need the same prefix, at least for MSVC.

Syntax error when choosing multiple UINT formats on MFC

I have the book Programming Windows with MFC, Second Edition, by Jeff Prosise, but in the very first example this error happens:
error C2146: syntax error : missing ')' before identifier '│'
In this line:
dc.DrawText(_T("Hello, MFC"), -1, &rect, DT_SINGLELINE │ DT_CENTER │ DT_VCENTER);
If I choose only one of those three formats, no error occurs, although, the program doesn't show the text as the book says it would, could anybody help? I've compiled other two examples in the book and they are compiling quite fine.
You've apparently typed the code incorrectly. That's supposed to be a vertical bar (which C and C++ use to mean "bitwise OR"). On US keyboards, that's typically immediately above the enter key (I suspect you may have entered one of the line drawing characters in the IBM extended character set instead, but it's hard to be sure).

Strange Error C2065: 'ERROR' : undeclared identifier

As part of a bigger project ( the ff activex plugin for Mozilla Firefox) there is this code snippet:
if (CombineRgn(hrgnClip, hrgnClip, hRGN, RGN_AND) != ERROR)
{
::InvalidateRgn(m_hWndParent, hrgnClip, fErase);
}
When I build in VS2012 I get "Error C2065: 'ERROR' : undeclared identifier"
ERROR is defined in wingdi.h like this:
...
/* Region Flags */
#define ERROR 0 // it wont build when this macro is used
#define NULLREGION 1 // it builds when this macro is used
#define SIMPLEREGION 2
#define COMPLEXREGION 3
#define RGN_ERROR ERROR
...
The strange thing is that if I replace ERROR (just to see if it builds OK) with NULLREGION or SIMPLEREGION (which are macros in the same file, just two lines below the offending one) in the if statement above, the code builds OK. When I use ERROR, the code wont build.
Is it possible that the ERROR macro defined above gets masked by some keyword or another macro or some such by Visual Studio?
The problem here is that ERROR actually appears in the compile error message. That should not happen, the preprocessor should have substituted it with 0.
So somewhere in a .h file you #included after windows.h, some programmer took the 10 second shortcut to a macro name collision problem and wrote this:
#undef ERROR
You'd need to find that line and remove it. That's likely to be difficult and liable to give you a maintenance headache since that's a file you don't own and might well be updated in the future, forcing you to make that change over and over again. The alternative is to redefine it yourself:
#define MYGDIERROR 0
...
if (CombineRgn(hrgnClip, hrgnClip, hRGN, RGN_AND) != MYGDIERROR)
//etc...
Which still gives you a maintenance problem and requires you taking a bet that the return value definition of CombineRgn() is never going to change. That's a very safe bet, GDI is cast in stone.

Visual Studio not recognizing certain classes

I keep getting the error:
error C2146: syntax error : missing ';' before identifier 'mCameraFrame'
for the line of code:
Frame mCameraFrame;
So clearly my frame class isn't being found somehow. I have the frame.h header file (which defines the Frame class) directly included in this file. Why doesn't visual studio recognize it?
The error is coming from previous lines of code, possibly in a header file.
For example:
struct foo
{
int a;
}
Frame mCameraFrame;
Notice the missing ; after the }? That makes the Frame legal as an instance of the structure, but now there's a missing ; before mCameraFrame, resulting in the kind of error you reported.
The compiler can't report a missing ; after the } because it has no way to know there's supposed to be one there, since the Frame that comes after it is perfectly legal.
It's not unusual for a single missing ; or missing } to result in errors reported many lines later than the actual problem, sometimes hundreds of them.
Figured I'd report back to anyone that's interested. The problem was that the Frame class that was supposed to be definining mCameraFrame was in a different namespace, so all I had to do was "using namespace ....;". Doh! :P

problem using formatted Fortran `write(6,*)` output

I'm currently porting an application from Fortran to C and need to output some variables to compare results. I'm very new to Fortran, and although i understand the code and have now ported several thousand lines, I'm a noob at writing Fortran code myself.
This code:
write(6,'(A,I3,A,E12.8,A,E12.8,A,E12.8,A,E12.8,A,E12.8)') 'iHyd:',
& ih,'; dzdr: ',dzdr,'; tauray:', tauRay,'; zRay: ',
& zray,'; ampRay: ',realpart(aray),'+j*',
& imagpart(aray),'; qRay: ',qray,'; width :',w
Compiles fine, but when run, the program exits with:
At line 296 of file calcpr.for (unit = 6, file = 'stdout')
Fortran runtime error: Expected INTEGER for item 15 in formatted transfer, got REAL
(A,I3,A,E12.8,A,E12.8,A,E12.8,A,E12.8,A,E12.8)
^
q0: 1432.3944878270595
nArrayR: 501 nArrayZ: 201
iHyd: 1; dzdr: ************; tauray:************; zRay: ************; ampRay: NaN+j* NaN
; qRay:
Besides being really ugly, it doesn't make much sense to me, as ìh is declared as integer*8 and not as real.
So how can i solve this?
I'm counting 6 character&variable specifications in the format statement, but you're printing 8 of them.
edit:
a nicer use of the format statement would be '(A,I3,7(A,E12.8))'
Fortran "recycles" the format if there are more things to be printed than specified in the format statement. If a write statement gives results you don't understand, to diagonose the problem it may be helpful to remove the things printed one at a time until the error goes away.
It says "item 15", which I would take to be down near the end of your list, not ih at the beginning. It's clear that both "w" and "qray" are being printed as REAL; is either one of them an INTEGER? You may need to change the format specifier then.