Sign in to follow this  
dvak

static members

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 once
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

...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

Share this post


Link to post
Share on other sites
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 once
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

...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?

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this