static member funcs/variables
-
OK, I've got some code which I cut and paste straight from the MSDN documentation on the static C++ keyword.....and it won't link :mad: can anyone work out why? This is the code:
// C++ only
class SavingsAccount
{
public:
static void setInterest( float newValue ) // Member function
{ currentRate = newValue; } // that accesses
// only static
// members
private:
char name[30];
float total;
static float currentRate; // One copy of this member is
// shared among all instances
// of SavingsAccount
};// Static data members must be initialized at file scope, even
// if private.
float SavingsAccount::currentRate = 0.00154;I copied it into a header file, and I'm getting the following errors
Tracker.obj : error LNK2005: "private: static float SavingsAccount::currentRate" (?currentRate@SavingsAccount@@0MA) already defined in Capture.obj
Tracker.obj : error LNK2005: "protected: static int CTracker::a" (?a@CTracker@@1HA) already defined in Capture.obj
Tracker.obj : warning LNK4006: "private: static float SavingsAccount::currentRate" (?currentRate@SavingsAccount@@0MA) already defined in Capture.obj; second definition ignoredBy the way, this isn't just me nitpicking at MSDN, I have a very similar class that throws up the same errors, and I need to know why! TIA, Pete
-
OK, I've got some code which I cut and paste straight from the MSDN documentation on the static C++ keyword.....and it won't link :mad: can anyone work out why? This is the code:
// C++ only
class SavingsAccount
{
public:
static void setInterest( float newValue ) // Member function
{ currentRate = newValue; } // that accesses
// only static
// members
private:
char name[30];
float total;
static float currentRate; // One copy of this member is
// shared among all instances
// of SavingsAccount
};// Static data members must be initialized at file scope, even
// if private.
float SavingsAccount::currentRate = 0.00154;I copied it into a header file, and I'm getting the following errors
Tracker.obj : error LNK2005: "private: static float SavingsAccount::currentRate" (?currentRate@SavingsAccount@@0MA) already defined in Capture.obj
Tracker.obj : error LNK2005: "protected: static int CTracker::a" (?a@CTracker@@1HA) already defined in Capture.obj
Tracker.obj : warning LNK4006: "private: static float SavingsAccount::currentRate" (?currentRate@SavingsAccount@@0MA) already defined in Capture.obj; second definition ignoredBy the way, this isn't just me nitpicking at MSDN, I have a very similar class that throws up the same errors, and I need to know why! TIA, Pete
this line:
// Static data members must be initialized at file scope, even
// if private.
float SavingsAccount::currentRate = 0.00154;Needs to be placed into your .cpp file. Currently it is in your header file. When it gets included into multiple files the linker tries to allocate multiple instances of the static variable, and that is why you are seeing that probrlm.
-
this line:
// Static data members must be initialized at file scope, even
// if private.
float SavingsAccount::currentRate = 0.00154;Needs to be placed into your .cpp file. Currently it is in your header file. When it gets included into multiple files the linker tries to allocate multiple instances of the static variable, and that is why you are seeing that probrlm.
Thanks! Ok, so if you have a static member variable then it has to be provided with a global definition, which is what that line does, right? But what if the variable is a user-defined type? I'm getting LNK2001 errors for all of the static variables of user-defined types in my class. I'm guessing it's because VC++ doesn't know how to initialize them correctly. So here's what I've tried so far - The variables are declared in my header file as follows:
class CTracker
{
...
protected:
...
static C2DPoint **m_aCentroids;
static double ***m_aCentroidDirs;
static UINT *m_aNumPoints;
...
};I then initialize them in my cpp file like this:
static C2DPoint CTracker::**m_aCentroids = NULL;
static double CTracker::***m_aCentroidDirs = NULL;
static UINT CTracker::*m_aNumPoints = NULL;but i'm still getting errors like the following:
error LNK2001: unresolved external symbol "protected: static class C2DPoint * * CTracker::m_aCentroids" (?m_aCentroids@CTracker@@1PAPAVC2DPoint@@A)
Any idea why? Oh, and thanks again for your help so far (with this /and/ the function pointer thing!). Pete
-
Thanks! Ok, so if you have a static member variable then it has to be provided with a global definition, which is what that line does, right? But what if the variable is a user-defined type? I'm getting LNK2001 errors for all of the static variables of user-defined types in my class. I'm guessing it's because VC++ doesn't know how to initialize them correctly. So here's what I've tried so far - The variables are declared in my header file as follows:
class CTracker
{
...
protected:
...
static C2DPoint **m_aCentroids;
static double ***m_aCentroidDirs;
static UINT *m_aNumPoints;
...
};I then initialize them in my cpp file like this:
static C2DPoint CTracker::**m_aCentroids = NULL;
static double CTracker::***m_aCentroidDirs = NULL;
static UINT CTracker::*m_aNumPoints = NULL;but i'm still getting errors like the following:
error LNK2001: unresolved external symbol "protected: static class C2DPoint * * CTracker::m_aCentroids" (?m_aCentroids@CTracker@@1PAPAVC2DPoint@@A)
Any idea why? Oh, and thanks again for your help so far (with this /and/ the function pointer thing!). Pete
In your CPP file, you don't need the static on the front, and I think you've got the *s in the wrong place:
C2DPoint **CTracker::m_aCentroids = NULL;
double ***CTracker::m_aCentroidDirs = NULL;
UINT *CTracker::m_aNumPoints = NULL;Hope this helps.
Derek Waters
derek@lj-oz.com -
In your CPP file, you don't need the static on the front, and I think you've got the *s in the wrong place:
C2DPoint **CTracker::m_aCentroids = NULL;
double ***CTracker::m_aCentroidDirs = NULL;
UINT *CTracker::m_aNumPoints = NULL;Hope this helps.
Derek Waters
derek@lj-oz.com