static members

Started by
7 comments, last by dvak 15 years, 5 months ago
hello CollectiveMind, Can/HowDo I assign static class member variabls in a class.constructor or class.setter function? If so, is there any special needs if the class.member variable is a pointer? I ask cause im getting the error below.
Quote: error LNK2001: unresolved external symbol "public: static class HE_Node::HE_Graphics * HE_Node::graphicsPtr" (?graphicsPtr@HE_Node@@2PAVHE_Graphics@1@A) HE_Node.obj
Below (*.cpp) is the code that's giving me the error.

#include "StdAfx.h"
#include "HE_Node.h"

HE_Node::HE_Node(void)
{
	//Can't get this static class member assignment to work!!!
	graphicsPtr = 0;  //Cant get this to werk. not sure how to access static data member here.	
}

HE_Node::~HE_Node(void)
{
}
void HE_Node::setGraphicsPtr(HE_Graphics &c_graphicsPtr)
{
	//Can't get this LINE to work EITHER!!!
	graphicsPtr = &c_graphicsPtr;
}

...snippetEnd


Below (*.h) is where Ive created my STATIC pointer.

class HE_Node
{
public:

	class HE_Graphics; //declare forward
	static HE_Graphics *graphicsPtr;	
	//static void setGraphicsPtr(HE_Graphics &c_graphicsPtr); //TODO: Cant get this to work

...snippetEnd

vcpp 2008 in XP.sp1 intel mac
Advertisement
you need to initialise your static variable

try to add just before your constructor :

HE_Graphics * HE_Node::graphicsPtr = NULL;

You actually have only declared the static pointer, but don't create (allocate memory) for it until you initialize it (as previous poster said). Just add that line to the top of the .cpp file before the constructor and you should be ok.

Cheers,

Bob

[size="3"]Halfway down the trail to Hell...
placing the suggested code above "HE_Graphics *HE_Node::graphicsPtr = NULL;" my constructor in the *.cpp class file gives me the error below.

Quote:
error C2371: 'graphicsPtr' : redefinition; different basic types


I thought a purpose for constructors was to initilize variables. Wouldn't the constructor be the proper place for initializing this static variable?

The point of this is to get all HE_Node derived classes to "see" my HE_Graphics instance/object. Is this not the proper way to do this?

[Edited by - dvak on November 20, 2008 10:57:33 PM]
You declared it with the wrong type. If HE_Graphics is a member class of HE_Node, you need to specify that in the declaration:
HE_Node::HE_Graphics *HE_Node::graphicsPtr = NULL;
You did mean for that to be a member type, right?

Any "initialization" of static members that you do in the constructor will get executed every time you create an HE_Node. In the case of your code, this means that the pointer gets nullified every time you create a new instance.
Quote:Original post by theOcelot
You declared it with the wrong type. If HE_Graphics is a member class of HE_Node, you need to specify that in the declaration:
HE_Node::HE_Graphics *HE_Node::graphicsPtr = NULL;
You did mean for that to be a member type, right?

Any "initialization" of static members that you do in the constructor will get executed every time you create an HE_Node. In the case of your code, this means that the pointer gets nullified every time you create a new instance.


In addition, initializing a static variable in the constructor also means that until you don't create the first object, that variable remains in an undefined state, and since static variables are meant to be used without the need of an instance, this would break the code.
ah - OK. so i DONT want it in the constructor. i guess all the better reason to put it in a class.setter function.

HE_Graphics is not derived from HE_Node.

So with the code below (with nothing in the constructor) i still get the original error.

HE_Node.h
#pragma onceclass HE_Node{public:		class HE_Graphics; //declare forward	static HE_Graphics *graphicsPtr;		static void setGraphicsPtr(HE_Graphics &c_graphicsPtr); //TODO: Cant get this to work...snippet END


HE_Node.cpp
#include "StdAfx.h"#include "HE_Node.h"HE_Node::HE_Node(void){	}HE_Node::~HE_Node(void){} void HE_Node::setGraphicsPtr(HE_Graphics &c_graphicsPtr){	//Can't get this LINE to work!!!        // HE_Node::HE_Graphics *HE_Node::graphicsPtr = &c_graphicsPtr	graphicsPtr = &c_graphicsPtr; //nor this one.}...snippet END
ah - OK. so i DONT want it in the constructor. i guess all the better reason to put it in a class.setter function.

HE_Graphics is not derived from HE_Node.

So with the code below (with nothing in the constructor) i still get the original error.

HE_Node.h
#pragma onceclass HE_Node{public:		class HE_Graphics; //declare forward	static HE_Graphics *graphicsPtr;		static void setGraphicsPtr(HE_Graphics &c_graphicsPtr); //TODO: Cant get this to work...snippet END


HE_Node.cpp
#include "StdAfx.h"#include "HE_Node.h"HE_Node::HE_Node(void){	}HE_Node::~HE_Node(void){} void HE_Node::setGraphicsPtr(HE_Graphics &c_graphicsPtr){	//Can't get this LINE to work!!!        // HE_Graphics *HE_Node::graphicsPtr = &c_graphicsPtr	graphicsPtr = &c_graphicsPtr; //nor this one.}...snippet END


Secondly, why would i need "HE_Node::" if were already within the class scope?
AH- got the answer to finally make sense.
this link also helped. Thanks guys!!

Quote:
You generally have to define static members in the CPP file to keep the definition at file scope instead of class scope. Putting the definition into an H file means the definition will get repeated into file scope of all files that include the H file; causing a different linker error.


http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/379cb6b5-3982-45c2-9941-120ec4aa7fe2/

This topic is closed to new replies.

Advertisement