Discussion:
C2275: illegal use of this type as an expression? It's a DWORD?
(too old to reply)
k***@gmail.com
2007-06-06 01:02:22 UTC
Permalink
Raw Message
I'm trying out some of the sample code on Raymond Chen's blog:

http://blogs.msdn.com/oldnewthing/archive/2003/08/29/54728.aspx

Unfortunately I can't get the following snippet of code to compile:

void OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
if (!IsRectEmpty(&ps.rcPaint))
{
// compute time to next update - we update once a second
SYSTEMTIME st;
GetSystemTime(&st);

DWORD dwTimeToNextTick = 1000 - st.wMilliseconds;
SetTimer(hwnd, 1, dwTimeToNextTick,
InvalidateAndKillTimer);
}
PaintContent(hwnd,&ps);
EndPaint(hwnd, &ps);
}

Here is the compiler output:
error C2275: 'DWORD' : illegal use of this type as an expression see
declaration of 'DWORD'
error C2146: syntax error : missing ';' before identifier
'dwTimeToNextTick'
error C2065: 'dwTimeToNextTick' : undeclared identifier

I'm using the Win32 project template in VS 2005.

Any ideas?

-Thx
k***@gmail.com
2007-06-06 01:14:01 UTC
Permalink
Raw Message
Post by k***@gmail.com
http://blogs.msdn.com/oldnewthing/archive/2003/08/29/54728.aspx
void OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
if (!IsRectEmpty(&ps.rcPaint))
{
// compute time to next update - we update once a second
SYSTEMTIME st;
GetSystemTime(&st);
DWORD dwTimeToNextTick = 1000 - st.wMilliseconds;
SetTimer(hwnd, 1, dwTimeToNextTick,
InvalidateAndKillTimer);
}
PaintContent(hwnd,&ps);
EndPaint(hwnd, &ps);
}
error C2275: 'DWORD' : illegal use of this type as an expression see
declaration of 'DWORD'
error C2146: syntax error : missing ';' before identifier
'dwTimeToNextTick'
error C2065: 'dwTimeToNextTick' : undeclared identifier
I'm using the Win32 project template in VS 2005.
Any ideas?
-Thx
I should also add that used the 'Empty Project' option..I kind want to
keep the sample as light as possible.
Ulrich Eckhardt
2007-06-06 06:31:11 UTC
Permalink
Raw Message
Post by k***@gmail.com
void OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
if (!IsRectEmpty(&ps.rcPaint))
{
// compute time to next update - we update once a second
SYSTEMTIME st;
GetSystemTime(&st);
DWORD dwTimeToNextTick = 1000 - st.wMilliseconds;
SetTimer(hwnd, 1, dwTimeToNextTick,
InvalidateAndKillTimer);
}
PaintContent(hwnd,&ps);
EndPaint(hwnd, &ps);
}
AFAICT, this code is valid C++ and C, although it is really bad because
completely lacks error checking. However, for this to compile as C, you
would need a C99 compliant compiler that allows declarations in the middle
of a block as opposed to only at the beginning.
Post by k***@gmail.com
error C2275: 'DWORD' : illegal use of this type as an expression see
declaration of 'DWORD'
error C2146: syntax error : missing ';' before identifier
'dwTimeToNextTick'
error C2065: 'dwTimeToNextTick' : undeclared identifier
I'm using the Win32 project template in VS 2005.
Looks like you you neither use a C++ nor a C99 compiler.

Uli
Tim Roberts
2007-06-07 07:09:07 UTC
Permalink
Raw Message
Post by Ulrich Eckhardt
Post by k***@gmail.com
I'm using the Win32 project template in VS 2005.
Looks like you you neither use a C++ nor a C99 compiler.
Yes, he said that. He was using VS2005, which is not a C99 compiler.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
SvenC
2007-06-06 07:49:22 UTC
Permalink
Raw Message
Hi,
Post by k***@gmail.com
http://blogs.msdn.com/oldnewthing/archive/2003/08/29/54728.aspx
error C2275: 'DWORD' : illegal use of this type as an expression see
declaration of 'DWORD'
error C2146: syntax error : missing ';' before identifier
'dwTimeToNextTick'
error C2065: 'dwTimeToNextTick' : undeclared identifier
I'm using the Win32 project template in VS 2005.
Have you added #include <windows.h> ?

--
SvenC
k***@gmail.com
2007-06-06 11:02:15 UTC
Permalink
Raw Message
Post by SvenC
Hi,
Post by k***@gmail.com
http://blogs.msdn.com/oldnewthing/archive/2003/08/29/54728.aspx
error C2275: 'DWORD' : illegal use of this type as an expression see
declaration of 'DWORD'
error C2146: syntax error : missing ';' before identifier
'dwTimeToNextTick'
error C2065: 'dwTimeToNextTick' : undeclared identifier
I'm using the Win32 project template in VS 2005.
Have you added #include <windows.h> ?
--
SvenC
Yup...I have one file in the project based on the following scratch
program:

#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <ole2.h>
#include <commctrl.h>
#include <shlwapi.h>

HINSTANCE g_hinst; /* This application's
HINSTANCE */
HWND g_hwndChild; /* Optional child window
*/

/*
* OnSize
* If we have an inner child, resize it to fit.
*/
void
OnSize(HWND hwnd, UINT state, int cx, int cy)
{
if (g_hwndChild) {
MoveWindow(g_hwndChild, 0, 0, cx, cy, TRUE);
}
}

/*
* OnCreate
* Applications will typically override this and maybe even
* create a child window.
*/
BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
return TRUE;
}

/*
* OnDestroy
* Post a quit message because our application is over when the
* user closes this window.
*/
void
OnDestroy(HWND hwnd)
{
PostQuitMessage(0);
}

/*
* PaintContent
* Interesting things will be painted here eventually.
*/
void
PaintContent(HWND hwnd, PAINTSTRUCT *pps)
{
}

/*
* OnPaint
* Paint the content as part of the paint cycle.
*/
void
OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
PaintContent(hwnd, &ps);
EndPaint(hwnd, &ps);
}

/*
* OnPrintClient
* Paint the content as requested by USER.
*/
void
OnPrintClient(HWND hwnd, HDC hdc)
{
PAINTSTRUCT ps;
ps.hdc = hdc;
GetClientRect(hwnd, &ps.rcPaint);
PaintContent(hwnd, &ps);

}

/*
* Window procedure
*/
LRESULT CALLBACK
WndProc(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
switch (uiMsg) {

HANDLE_MSG(hwnd, WM_CREATE, OnCreate);
HANDLE_MSG(hwnd, WM_SIZE, OnSize);
HANDLE_MSG(hwnd, WM_DESTROY, OnDestroy);
HANDLE_MSG(hwnd, WM_PAINT, OnPaint);
case WM_PRINTCLIENT: OnPrintClient(hwnd, (HDC)wParam); return 0;
}

return DefWindowProc(hwnd, uiMsg, wParam, lParam);
}

BOOL
InitApp(void)
{
WNDCLASS wc;

wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = g_hinst;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Scratch";

if (!RegisterClass(&wc)) return FALSE;

InitCommonControls(); /* In case we use a common
control */

return TRUE;
}

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,
LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
HWND hwnd;

g_hinst = hinst;

if (!InitApp()) return 0;

if (SUCCEEDED(CoInitialize(NULL))) {/* In case we use COM */

hwnd = CreateWindow(
"Scratch", /* Class Name */
"Scratch", /* Title */
WS_OVERLAPPEDWINDOW, /* Style */
CW_USEDEFAULT, CW_USEDEFAULT, /* Position */
CW_USEDEFAULT, CW_USEDEFAULT, /* Size */
NULL, /* Parent */
NULL, /* No menu */
hinst, /* Instance */
0); /* No special parameters
*/

ShowWindow(hwnd, nShowCmd);

while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

CoUninitialize();
}

return 0;
}
Kim Gräsman
2007-06-06 11:08:49 UTC
Permalink
Raw Message
Hi,
Post by k***@gmail.com
Yup...I have one file in the project based on the following scratch
To follow up on Ulrich's response, and alluding to a related thread: is this
a .c or .cpp file?

That is, does it compile with the C++ compiler or the C compiler?

- Kim
k***@gmail.com
2007-06-06 12:36:28 UTC
Permalink
Raw Message
Kim Gräsman
2007-06-06 13:00:04 UTC
Permalink
Raw Message
Hi,
Is there even a way to compile "C compliant" code in VS 2005. Or do
you always have to use a C++ compiler?
Why don't they both work? This is more of a curiosity on my part.
This is what Ulrich alluded to, you can compile C code just fine, it's just
that VS 2005 doesn't fully implement C99 (it has support for variadic macros,
for example, but apparently not variable declaration other than at the top
of function scope, both of which are part of C99).

Hope that makes sense.

- Ki
k***@gmail.com
2007-06-06 13:34:59 UTC
Permalink
Raw Message
Tim Roberts
2007-06-07 07:10:18 UTC
Permalink
Raw Message
I had no idea that old versions of C (pre-99) did not support that
kind of variable declaration.
Really? I'm actually fascinated by that response. I had no idea that
there are C programmers today who simply ASSUME that there environments are
C99-compliant.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Annon Hong
2011-07-04 23:14:13 UTC
Permalink
Raw Message
Looks like as long as you declare all your variables before statements.

http://stackoverflow.com/questions/2099017/help-opening-a-file-in-visual-studio-2008
his
This is the issue. Thanks.
In VS if the source file ends in .c *it fails*. If the source file
ends in .cpp *it succeeds*.
Is there even a way to compile "C compliant" code in VS 2005. Or do
you always have to use a C++ compiler?
Why don't they both work? This is more of a curiosity on my part.
-Thx
Post by Kim Gräsman
Hi,
To follow up on Ulrich's response, and alluding to a related thread: is this
a .c or .cpp file?
That is, does it compile with the C++ compiler or the C compiler?
- Kim
I copied this answer from
http://stackoverflow.com/questions/2099017/help-opening-a-file-in-visual-studio-2008
so looks like as long as you have all your declaration of variables before any statements, then it compiles.
-----------------------------
You can only declare variables at the top of a scope {} before any code in C.
In C++ you can declare them anywhere. So either change your filename to test.cpp or move your first printf down below the declaration of FILE* fileptr.
int main(void )
{
FILE *filePtr;
printf("Hello");
-----------------------------
Annon Hong
2011-07-04 23:14:18 UTC
Permalink
Raw Message
Looks like as long as you declare all your variables before statements.

http://stackoverflow.com/questions/2099017/help-opening-a-file-in-visual-studio-2008
his
This is the issue. Thanks.
In VS if the source file ends in .c *it fails*. If the source file
ends in .cpp *it succeeds*.
Is there even a way to compile "C compliant" code in VS 2005. Or do
you always have to use a C++ compiler?
Why don't they both work? This is more of a curiosity on my part.
-Thx
Post by Kim Gräsman
Hi,
To follow up on Ulrich's response, and alluding to a related thread: is this
a .c or .cpp file?
That is, does it compile with the C++ compiler or the C compiler?
- Kim
I copied this answer from
http://stackoverflow.com/questions/2099017/help-opening-a-file-in-visual-studio-2008
so looks like as long as you have all your declaration of variables before any statements, then it compiles.
-----------------------------
You can only declare variables at the top of a scope {} before any code in C.
In C++ you can declare them anywhere. So either change your filename to test.cpp or move your first printf down below the declaration of FILE* fileptr.
int main(void )
{
FILE *filePtr;
printf("Hello");
-----------------------------
Post by Annon Hong
Looks like as long as you declare all your variables before statements.
http://stackoverflow.com/questions/2099017/help-opening-a-file-in-visual-studio-2008
Annon Hong
2011-07-04 23:14:35 UTC
Permalink
Raw Message
Looks like as long as you declare all your variables before statements.
his
This is the issue. Thanks.
In VS if the source file ends in .c *it fails*. If the source file
ends in .cpp *it succeeds*.
Is there even a way to compile "C compliant" code in VS 2005. Or do
you always have to use a C++ compiler?
Why don't they both work? This is more of a curiosity on my part.
-Thx
Post by Kim Gräsman
Hi,
To follow up on Ulrich's response, and alluding to a related thread: is this
a .c or .cpp file?
That is, does it compile with the C++ compiler or the C compiler?
- Kim
I copied this answer from
http://stackoverflow.com/questions/2099017/help-opening-a-file-in-visual-studio-2008
so looks like as long as you have all your declaration of variables before any statements, then it compiles.
-----------------------------
You can only declare variables at the top of a scope {} before any code in C.
In C++ you can declare them anywhere. So either change your filename to test.cpp or move your first printf down below the declaration of FILE* fileptr.
int main(void )
{
FILE *filePtr;
printf("Hello");
-----------------------------
Post by Annon Hong
Looks like as long as you declare all your variables before statements.
http://stackoverflow.com/questions/2099017/help-opening-a-file-in-visual-studio-2008
Post by Annon Hong
Looks like as long as you declare all your variables before statements.
http://stackoverflow.com/questions/2099017/help-opening-a-file-in-visual-studio-2008
m***@gmail.com
2014-06-04 12:47:30 UTC
Permalink
Raw Message
Post by k***@gmail.com
http://blogs.msdn.com/oldnewthing/archive/2003/08/29/54728.aspx
void OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
if (!IsRectEmpty(&ps.rcPaint))
{
// compute time to next update - we update once a second
SYSTEMTIME st;
GetSystemTime(&st);
DWORD dwTimeToNextTick = 1000 - st.wMilliseconds;
SetTimer(hwnd, 1, dwTimeToNextTick,
InvalidateAndKillTimer);
}
PaintContent(hwnd,&ps);
EndPaint(hwnd, &ps);
}
error C2275: 'DWORD' : illegal use of this type as an expression see
declaration of 'DWORD'
error C2146: syntax error : missing ';' before identifier
'dwTimeToNextTick'
error C2065: 'dwTimeToNextTick' : undeclared identifier
I'm using the Win32 project template in VS 2005.
Any ideas?
-Thx
Now if it's C you cannot declare veriables in the middle of function.

for example

void somethin(int decision)
{
if(decision != 0) return;
HANDLE g_create = CreateFile(BLAHBLAHBLAH);

CloseHandle(g_create);
}

it doesn't work but if you do like this

void somethin(int decision)
{
HANDLE g_create = CreateFile(BLAHBLAHBLAH);
if(decision != 0) return;

CloseHandle(g_create);
}

it will work,its difrences between c and c++

Loading...