Thread: Problems with static const class members

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109

    Problems with static const class members

    For some reason the following code gives me a "syntax error before numeric constant" error on compile in dev-c++

    Code:
    class Heuristic // abstract
    {
        public:
            virtual int distanceFromOptimal(State* b) = 0;
            virtual int optimalValue() = 0;
            virtual int optimalDirection() = 0;
            
            const static int INFINITY;
            static const int INCREASE;
            static const int DECREASE;
    };
    even though this code compiles with no errors

    Code:
    class State
    {
        public:
            const static string STATE_TYPE; // to identify problem specific states
    };
    Anyone got any ideas why?

    Also I've read in my c++ book that static variables have only file scope at most, does this affect class members in any way?
    Last edited by sigfriedmcwild; 12-04-2004 at 03:47 PM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Apparently INFINITY is already defined somewhere...Try not using all caps for your variable names...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    I used all caps since those are supposed to be constants (kind of like when you use all caps for defines)

    Still when I try to use any of those constants outside Heuristic class I get a "syntax error before ; token"

    code like this gives errors
    Code:
    return Heuristic.INCREASE;
    or
    Code:
    int a = Heuristic.DECREASE;
    since the variables are static I should be able to access them without constructing an object, using classname.varname right?
    Last edited by sigfriedmcwild; 12-04-2004 at 07:30 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >since the variables are static I should be able to access them without constructing an object
    Yes.

    >using classname.varname right?
    No. You would use classname::varname.
    My best code is written with the delete key.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Where do you initialize them with values? until you initialize a static member it doesn't exist.

    try
    Code:
    int Heuristic::INCREASE=0;
    Last edited by manofsteel972; 12-04-2004 at 07:54 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    thx prelude

    @manofsteel972 - I did do that in the .cpp file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM