oaidl.h(319): error C2057: expected constant expre

2019-08-04 02:58发布

问题:

I've recently switched from Visual Studio 2010 to Visual Studio 2012. The project I'm working on uses the BitwiseEnums library from MiLi. I'm including just the BitwiseEnums library as per the instructions here, so the only file being added to my project is bitwise_enums.h.

In VS2010 I had no issues. In VS2012 I'm getting the following error message whenever I #include bitwise_enums.h and try to compile:

1>c:\program files (x86)\windows kits\8.0\include\um\oaidl.h(319): 
error C2057: expected constant expression

When I check the external dependencies for my project, it does list the file oaidl.h. Looking inside this file, I find the following statement which ends on line 319.

typedef /* [v1_enum] */ 
enum tagSF_TYPE
    {
        SF_ERROR    = VT_ERROR,
        SF_I1   = VT_I1,
        SF_I2   = VT_I2,
        SF_I4   = VT_I4,
        SF_I8   = VT_I8,
        SF_BSTR = VT_BSTR,
        SF_UNKNOWN  = VT_UNKNOWN,
        SF_DISPATCH = VT_DISPATCH,
        SF_VARIANT  = VT_VARIANT,
        SF_RECORD   = VT_RECORD,
        SF_HAVEIID  = ( VT_UNKNOWN | VT_RESERVED ) 
    }   SF_TYPE; // Line 319

My questions are:

  1. What is oaidl.h and what is causing it to be included in my project?
  2. What does the error message mean, and how can I fix it?
  3. What could be a reason for this error only occurring in VS2012?

回答1:

I've managed to narrow this down to naming conflict of some kind between bitwise_enums.h and Windows.h, which I was also including in the project. I noticed that I didn't get the error if I included the entire MiLi library, because mili.h places everything inside a namespace using #define NAMESPACE_BEGIN and #define NAMESPACE_END, whereas defining them in the way suggested here, means that the contents of bitwise_enums.h end up polluting the global namespace. I am still not altogether clear on why this causes a problem, as I can't seem to locate the clash, nor am I clear on why it worked in VS2010. However, I was able to solve it by altering the #define instructions so that they create a namespace inside bitwise_enums.h:

#define NAMESPACE_BEGIN namespace Mili
#define NAMESPACE_END }
#include <bitwise_enums.h>
#undef NAMESPACE_BEGIN
#undef NAMESPACE_END

Another solution is to include mili.h and follow the second suggestion at this link, but since I only need one library, I prefer not to have to have lots of lines individually excluding the libraries I don't want.

Hopefully this answer will be useful if anyone in future ends up in the same obscure situation as myself, using that specific method of including a single library from MiLi, whilst also including Windows.h in VS2012 (probably an unlikely combination)!