How do i do this? (static structure inside class)

This is a discussion on How do i do this? (static structure inside class) within the C++ Programming forums, part of the General Programming Boards category; I get errors when i try this: Code: class Player: public Character { private: static const float JUMP_HEIGHT = 13; ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    How do i do this? (static structure inside class)

    I get errors when i try this:

    Code:
    class Player:
        public Character
    {
    private:
        static const float JUMP_HEIGHT = 13;
        static const int REGEN_DELAY = 100;
        static const int ALARM_REGENATE = 1;
        static const int ACTION_STAND = 0;
        static const int ACTION_DUCK = 1;
        static const int ACTION_WALK = 2;
        static const int ACTION_JUMP = 3;
        static const int ACTION_PUNCH = 4;
        static const int ACTION_KICK = 5;
        static const int ACTION_SIT = 6;
    
        struct
        {
            int imageCount, nextAction;
            float speed;
        } static const PLAYER_ACTIONS[] = {
            {1, -1, 0.0f},   //stand
            {1, -1, 0.0f},      //duck
            {6, -1, 0.5f},   //walk
            {1, -1, 0.0f},   //jump
            {4, ACTION_STAND, 0.4f},  //punch
            {4, ACTION_STAND, 0.3f}, //kick
            {1, -1, 0.0f},   //sit
        };
    
    ...
    };
    D:\Code\StickOnline\src\/Player.h:32: error: a brace-enclosed initializer is not allowed here before '{' token
    D:\Code\StickOnline\src\/Player.h:40: error: invalid in-class initialization of static data member of non-integral type `const Player::<anonymous struct>[]'
    The error occurs at " } static const PLAYER_ACTIONS[] = {"

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    shouldn't you do

    } ACTIONS;
    static const ACTIONS Player_Actions[] = {

    ??
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Hmm, should be able to use anonymous structures...?

    A different problem. For some reason i get "obj\Release\src\Player.o:Player.cpp.text+0x144): undefined reference to `Player::JUMP_HEIGHT'
    obj\Release\src\Player.o:Player.cpp.text+0x178): undefined reference to `Player::JUMP_HEIGHT'"

    Yet i don't get the same errors for the other static const's defined in the Player class.

    EDIT: If i change JUMP_HEIGHT to an int it works....my head hurts X.x
    Last edited by 39ster; 11-16-2008 at 08:31 PM.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Ok i figured out the answer for both questions. You can only initialize the value in the class header if it's an integer.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    As Visual C++'s compiler likes to say:
    "You can only initialize static const integral data inside the class."

    The rest you must initialize outside the class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21