c++c++11structcompiler-errorsin-class-initialization

ISO C++ forbids member initialization within a struct


I'm writing an emulator and decided to control input/output emulation within a struct:

struct callbacks
{
short LastFrequency = 9000;
 int *MMIO_RANGE1;
 short Cycle_LN = 65535 / LastFrequency;
 const char *STATUS_FLAGS[] =
 {
   "ACK",
   "NO_VIB",
   "DATA",
   "BYTEPACK",
   "WORDPACK"
 };
}

This code above looks fine to me and seems to obey all of the rules ... but I get the error message as stated in the title above. I searched around and people say that the error means different things ... but what is it?

The problem is pointed towards "LastFrequency".


Solution

  • Two things: add a 5 and a ;

    Also: make sure to compile with -std=c++11 (or -std=c++0x for older g++ versions, or the equivalent options for your compiler of choice), because in-class initializers are a C++11 feature.

    BIG WARNING: this code is NOT supported by gcc 4.6 and requires gcc >= 4.7.3

    struct callbacks
    {
    short LastFrequency = 9000;
     int *MMIO_RANGE1;
     short Cycle_LN = 65535 / LastFrequency;
     const char *STATUS_FLAGS[5] = // <-- 5 here
     {
       "ACK",
       "NO_VIB",
       "DATA",
       "BYTEPACK",
       "WORDPACK"
     };
    }; // <-- ; here
    
    int main() 
    {
    
    }
    

    Live Example.