error C2057: expected constant expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manuanchani
    New Member
    • Jan 2012
    • 1

    #1

    error C2057: expected constant expression

    struct x : public y
    {
    const static int iword_index = std::ios_base:: xalloc();
    }

    i'm getting the error C2057: expected constant expression,whil e compiling.
    please help me in solving this problem.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A static variable is not a member of a struct. It has the scope of the struct but declaring it inside the struct does not create the variable.

    You have to create ansd initialize the variable outside the struct and then you can refer to it from inside the struct:

    Code:
     const static int iword_index = std::ios_base::xalloc();
    struct y
    {
    
    };
    struct x : public y
     {
     //const static int iword_index = std::ios_base::xalloc();
     const static int iword_index;
     };

    Comment

    Working...