Where is the huge difference, which generates error C2360, in following two definitions?
switch (msg) {
case WM_PAINT:
HDC hdc;
hdc = BeginPaint(hWnd, &ps); // No error
break;
}
and
switch (msg) {
case WM_PAINT:
HDC hdc = BeginPaint(hWnd, &ps); // Error
break;
}
The first is legal and the second isn't. Skipping a declaration without an initializer is sometimes allowed, but never one with an initializer.
See Storage allocation of local variables inside a block in c++.
When a variable is declared in one case, the next case is technically still in the same scope so you could reference it there but if you hit that case without hitting this one first you would end up calling an uninitialised variable. This error prevents that.
All you need to do is either define it before the switch statement or use curly braces { } to make sure it goes out of scope before exiting a specific case.
switch (msg) {
case WM_PAINT:
{
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
}
break;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With