Search code examples
c++visual-studio-2008compiler-errorspreprocessor-directiverelease-mode

Compiler error in inactive #ifdef block


I am working on a c++ program that was compiled with visual studio 2013 but needs to be compiled with visual studio 2008 as well, both in release mode. I am using #ifdef blocks to add alternatives to functions/features that were not supported back then. The following code block is just an example:

struct someStruct
{
#ifdef _VS2008   // defined in preprocessor definition
    someStruct()
    {
        number = -1;
    }
    int number;
#else
    int number = -1;
#endif
    char* Text;
};

and I am getting the following compiler error on the line int number = -1.

error C2864: 'someStruct::number' : only static const integral data members can be initialized within a class

Since the code blocks under #else (in this case int number = -1) appear to be and should be inactive, why is the compiler generating errors about them?

Any input is appreciated!


Solution

  • The solution was found. The code blocks are in a DLL project and the main project includes some of the exported headers from it. I didn't know I had to define _VS2008 in the main project as well.