static class members

Started by
6 comments, last by Abstraction 22 years, 5 months ago
I have this class declaration residing in my own Gui.h file that looks something like this (really its a lot bigger):
    
class BUTTON
{
private:
     
     static *int mx;
     static *int my;
     static DIMOUSESTATE *mstate

public:

     DWORD SetInput(int *mousex, int *mousey, 
                    DIMOUSESTATE *mousestate);
};
  
Then in my Gui.cpp file I delcare SetInput like this:
        
DWORD BUTTON::SetInput(int *mousex, int *mousey,
                       DIMOUSESTATE *mousestate)
{
     mx = mousex;
     my = mousey;
     mstate = mousestate;
     return(BUTTON_GOOD);
}
  
Everything compiles fine. Then when I try to execute my program I get linker errors in Gui.obj saying mx,my, and msate are unresolved symbols. I included Gui.h in Gui.cpp. I have like 10 member function that belong to the BUTTON class that are all declared in Gui.h and then defined in Gui.cpp. All those worked fine but as soon as I added the static members to the class and the SetInput member function I got shutdown. If I take the definition of SetInput and cram it inside the actual class declaration like this:
        
class BUTTON
{
private:
     
     static *int mx;
     static *int my;
     static DIMOUSESTATE *mstate

public:

     DWORD SetInput(int *mousex, int *mousey, 
                    DIMOUSESTATE *mousestate) 
     {
          mx = mousex;
          my = mousey;
          mstate = mousestate;
          return(BUTTON_GOOD)
     }
};
  
everything works fine. So what's the deal! Why can't I use static members of the class in member functions that are defined outside of the class. I'm using MSVC++ 6.0 Thank you very much. Edited by - Abstraction on October 31, 2001 2:43:04 PM
Advertisement
Since my, mx and mstate are static, there is one copy shared by all instances of the class. What''s their value before any objects are created? What''s their value before SetInput() is called by any of the objects?

Undefined.

You must define all static data members and methods at file scope:
// in your Gui.h _OR_ Gui.cpp:int *BUTTON::mx = NULL;int *BUTTON::my = NULL;DIMOUSESTATE BUTTON::mousestate = NULL; 

Note also that *int mx is not a valid declaration. static is not a datatype but a modifier, so you can''t have a pointer to static (which is what static *int mx; attempts to declare). Write instead:
static int *mx; 
in your gui.cpp file, insert this:

int BUTTON::*mx;
int BUTTON::*my;
DIMOUSESTATE BUTTON::*mstate;

this should fix it because when you use static variables in a class, you are just declaring them, so elsewhere you find define (allocate memory for) them.
Holly geez!

This has been bothering me for like 8 months. Thanks a bunch for you help. I can''t believe someone replied so fast!

By the way "*int mx" was just a typo
Whoa...wait just a minute. Both you guys said diffirent things:

int BUTTON::*mx;
int BUTTON::*my;
DIMOUSESTATE BUTTON::*mstate;

int *BUTTON::mx;
int *BUTTON::my;
DIMOUSESTATE *BUTTON::mstate;

So can I do it both ways or what? I have a C++ book but it only talks about static variables for 2 pragraphs. Static variables within classes are memtioned for like one sentence. Aside from that I have another proplem on the same subject (I never got the first deal to work). This time I''ll give real chunks of code not simplified versions.

Int Gui.h I have this:

  class CURSOR{private:    static LPDIRECTDRAW7 *directdraw; // Talk to directdraw    static LPDIRECTDRAWSURFACE *canvas; // Blt icon on this    static DIMOUSESTATE *mstate; // Talk to directinput    //...skip some stuff...public:   //...skip some more stuff...   DWORD Interface(LPDIRECTDRAW7 *ddraw,                    LPDIRECTDRAWSURFACE7 *surface,                    DIMOUSESTATE *mousestate);   //...skipping...}; // End CURSOR dec  


And in Gui.cpp

  // Globals ////////////////////////////////////////#include "Input.h"#include "Gui.h"//...skipping...LPDIRECTDRAW7        CURSOR::*directdraw = NULL; LPDIRECTDRAWSURFACE7 CURSOR::*canvas     = NULL; DIMOUSESTATE         CURSOR::*mstate     = NULL; //...skipping...    // Functions //////////////////////////////////////DWORD Interface(LPDIRECTDRAW7 *ddraw,                LPDIRECTDRAWSURFACE7 *surface,                 DIMOUSESTATE *mousestate){    directdraw = ddraw;    canvas     = surface;    mstate     = mousestate;    return(GUI_GOOD);} // End CURSOR::Interface  


So I compile and get this (3 times):
C:\MSVC\Math\source\Gui.cpp(28) : error C2440: ''='' : cannot convert from ''struct IDirectDraw7 ** '' to ''struct IDirectDraw7 *CURSOR::* ''
There is no context in which this conversion is possible

And if I change the static var stuff in Gui.cpp to this:

  LPDIRECTDRAW7        *CURSOR::directdraw = NULL; LPDIRECTDRAWSURFACE7 *CURSOR::canvas     = NULL; DIMOUSESTATE         *CURSOR::mstate     = NULL;   


the compiler gives me this (3 times):
C:\MSVC\Math\source\Gui.cpp(28) : error C2065: ''directdraw'' : undeclared identifier
C:\MSVC\Math\source\Gui.cpp(28) : error C2440: ''='' : cannot convert from ''struct IDirectDraw7 ** '' to ''int''

WTF

So I ask again, what I''m I doing wrong?





Never mind. I figured it out.

I did not put CURSOR:: in front of the Interface definition

Its amazing how coding for days turns your brain into mush - oozing out off your eyeballs so that even the most obvious mistake becames impossible.
quote:Original post by Abstraction
Never mind. I figured it out.

Comments like this are music to my ears. I find too many wont seek the solutions themselves, so I congratulate you.

Keep pushing pixels!
I just wanted to chime in, since this is an odd piece of code to see
  LPDIRECTDRAW7        *CURSOR::directdraw = NULL;  

You realize that''s a handle (as in a pointer to a pointer to a DD7 interface), not a pointer to DD7 interface?

LPDIRECTDRAW7 -> IDirectDraw7*

This would likely explode if you tried to use the creation functions with it, so I can only assume you''re setting it equal to the address of a DD7 pointer.

Another way to do this, is to copy the pointer and increase the reference count on the DD7 interface.

  class CCursor	{	Init(LPDIRECTDRAW7 pDD7)		{		_ASSERT(pDD7);		m_pDD7 = pDD7;		m_pDD7->AddRef();		}	Shutdown()		{		if(m_pDD7)			{			m_pDD7->Release();			m_pDD7 = 0;			}	~CCursor()		{		Shutdown();		}		LPDIRECTDRAW7 m_pDD7;	};  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement