Thread: I cannot for the life of me figure out why static int member variables wont' work

  1. #1
    Shadow12345
    Guest

    I cannot for the life of me figure out why static int member variables wont' work

    I have a public static int as one of my member variables for a class, but when I go to increment it (I can only have 8 instances of this class) the compiler gives me a LNK2001 error.

    I have written the code exactly how it is written in a C++ book but for some reason it still will not work;

    Code:
    class Light {
    public:
    Light();	//DEFAULT CONSTRUCTOR, THIS WILL MAKE THE LIGHT FULLBRIGHT, MEANING COMPLETELY WHITE
    Light(color_t*, material_t*, location_t*);	//THIS IS THE CONSTRUCTOR THAT WILL ALLOW FOR ALL OF THE PARAMETERS OF THE LIGHT TO BE SET
    void Activatate();
    void Deactivate();
    short Number;	
    static int NumberOfLights;	//FOR NOW WE WILL NOT BE ABLE TO CREATE ANY MORE THAN 8 LIGHTS UNTIL WE CAN CULL THE REST
    private:
    	color_t *Color;
    	material_t *Material;
    	location_t *Location;
    	
    };
    That is the class interface file, and the constructor simply increments it. Please help me fix this stupid little problem.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Okay, the class is written as it is in the book.

    A static member is 'static' only within the class.

    Admittedly, it's me, but I don't see where the constructor increments/decrements 'NumberOfLights'. There's not a single function definition in the class, only declarations.

    Sorry.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Shadow12345
    Guest
    *slaps self in the forehead* doh, I told you everything you need to know about what's in the implementation files: the constructor increments the static int member variable, it is called **cutting down on crap to post** so in short you are correct you do not see the definitions there, because I didn't post them, because I am afraid SALEM will eat me.

    Oh and when I say I wrote it exactly how it was written in the book, I mean I wrote it how the book did it, meaning I declared the type to be a static int, but that code isn't copied from a book. I try to stay away from copying code from a book exactly as it was written. Okay whew, I just had to clear that up.
    Last edited by Shadow12345; 11-19-2002 at 07:14 PM.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    class Light {
    public:
    Light();	//DEFAULT CONSTRUCTOR, THIS WILL MAKE THE LIGHT FULLBRIGHT, MEANING COMPLETELY WHITE
    Light(color_t*, material_t*, location_t*);	//THIS IS THE CONSTRUCTOR THAT WILL ALLOW FOR ALL OF THE PARAMETERS OF THE LIGHT TO BE SET
    void Activatate();
    void Deactivate();
    short Number;	
    static int NumberOfLights;	//FOR NOW WE WILL NOT BE ABLE TO CREATE ANY MORE THAN 8 LIGHTS UNTIL WE CAN CULL THE REST
    private:
    	color_t *Color;
    	material_t *Material;
    	location_t *Location;
    	
    };
    
    int Light::NumberOfLights = 0; //..needs initialization...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    No, Salem can kill you, but not eat you. That's illegal!

    Also, stop slapping yourself. If we all did that, we'd throw the Earth out of its rotational axis!

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Shadow12345
    Guest
    Okay thanks seb

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM