Search code examples
c++compiler-errorsbooleanvirtual

if, else producing error C2181: illegal else without if


I have a function with the following definition:

virtual bool Process(wtdaFileHandler &daHandler, wtdaGather &daGather);

All code paths in the this function return a bool and I am certainly calling this function, not one in a sub class. The following code produces the error in the posting title:

wtdaLFDProcess process;

// call some methods to do initialize process and args.

if (process.Process(daLFDFileHandler, daGather));
{
     retval = 0;
}
else
{
    retval = LFD_FILE_LOCK_ERROR;
    cout << "Could not obtain file lock for processing." << endl;
    WTDA_STATUS(3,  "Error...Stopping" );
    return;
}

Can anyone explain this? Maybe it's some caveat of C++ which I am unaware of? It makes no sense to me. The error undoubtedly refers to the else. I've built to make sure it's not just the intellisense. This is a Win32 C++ project.


Solution

  • You need to lose this semi-colon:

    if (process.Process(daLFDFileHandler, daGather));
                                                    ^
    

    The semi-colon detatches the if conditional from the following block, which is then interpreted as a scope, followed by an orphan else.