(Another Visual C++ 2008 Express Edition related post.)
If something is wrong within the if expression, you may wrongly receive the indication "error C2181: illegal else without matching if".
Example:
To resolve this problem, do a search for 'old' and replace it with 'primary_thread'.
If something is wrong within the if expression, you may wrongly receive the indication "error C2181: illegal else without matching if".
Example:
class primary_thread_tThis is my typical copy-paste scenario. I copy code from one source which uses another name for a class, namely old_t, to a new project which uses another name for a class of the same function, namely primary_thread_t. Although the error lies with the expression within the if statement (the identifier is not known), the compiler claims that the error lies with the structure of the if--else if--else structure.
{
public:
LRESULT message (HWND const & window, UINT const & message, WPARAM const & W, LPARAM const & L)
{
LRESULT result;
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT PS;
HDC const DC (BeginPaint (window, &PS));
if (DC)
{
}
BOOL (EndPaint (window, &PS)); // Just to show there's a return value.
result = 0;
break;
}
case WM_DESTROY:
{
PostQuitMessage (0);
result = 0;
break;
}
case WM_CREATE:
{
result = 0;
break;
}
default:
{
result = DefWindowProc (window, message, W, L);
break;
}
}
return result;
}
};
static LRESULT CALLBACK window_procedure (HWND const window, UINT const message, WPARAM const W, LPARAM const L)
{
LRESULT result; // Initialized before return statement.
if (old_t* const old = reinterpret_cast <old_t*> (GetWindowLongPtr (window, GWLP_USERDATA)))
{
result = old->message (window, message, W, L);
}
else if (message == WM_NCCREATE)
{
SetWindowLongPtr (window, GWLP_USERDATA, reinterpret_cast <LONG_PTR> (reinterpret_cast <CREATESTRUCT*> (L)->lpCreateParams));
result = DefWindowProc (window, message, W, L);
}
else
{
result = DefWindowProc (window, message, W, L);
}
return result;
}
To resolve this problem, do a search for 'old' and replace it with 'primary_thread'.