0

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!

Thomas Hsieh
  • 731
  • 1
  • 6
  • 26
  • Because `_VS2008`, contrary to your expectations, is not defined. Incidentally, if you have to support a pre-C++11 compiler it doesn't make much sense to write double implementations of every C++11 shortcut you take - you are just making the code harder to read and risk to have the two implementations going out of sync. – Matteo Italia Jul 24 '16 at 15:35
  • @MatteoItalia Sorry I didn't clarify this, but `_VS2008` I actually defined it in the preprocessor definition. I was asked to use the ifdefs and I believe it's because this application is rarely used and it's only updated once every few years. I guess I just had to quickly get this over with. Thanks for the advice though! – Thomas Hsieh Jul 25 '16 at 00:10

2 Answers2

1

You should use _MSC_VER macro to detect Visual Studio version:

#if (_MSC_VER == 1500)

1500 here means VS2008. The list of versions is here.

Community
  • 1
  • 1
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
  • Thanks for the information. I actually found that post too but I'm still getting errors. I'll update my question with the error I'm getting. – Thomas Hsieh Jul 23 '16 at 03:30
0

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.

Thomas Hsieh
  • 731
  • 1
  • 6
  • 26