Gaston Gonzalez Gaston Gonzalez - 2 days ago 3
C++ Question

error C2181: illegal else without matching if

Hello someone know why i get this error ?
error C2181: illegal else without matching if

if( pItemElem
#ifdef __SYS_MOUNT
&& pItemElem->GetPropA()->dwItemKind3 != IK3_MOUNT
#endif
)
#ifdef __SYS_MOUNT
else if (pItemElem && pItemElem->GetPropA()->dwItemKind3 ==
IK3_MOUNT)
m_dwMountItemID = pItemElem->m_dwItemId;
#endif

Answer

Assuming __SYS_MOUNT is defined, you have

if( pItemElem && pItemElem->GetPropA()->dwItemKind3 != IK3_MOUNT)
else if (pItemElem && pItemElem->GetPropA()->dwItemKind3 == IK3_MOUNT)
    m_dwMountItemID = pItemElem->m_dwItemId;

There is no body for the first if, the compiler will interpret it as

 if( pItemElem && pItemElem->GetPropA()->dwItemKind3 != IK3_MOUNT)
 {
     else if (pItemElem && pItemElem->GetPropA()->dwItemKind3 == IK3_MOUNT)
     {
         _dwMountItemID = pItemElem->m_dwItemId;
     }
 }

Written this way, it's obvious that the else if doesn't match to any if. You need to give a body for the if, even if it's empty. You can use {} or ;.