Click to See Complete Forum and Search --> : Problem with static class-element


AlionSolutions
April 25th, 2003, 06:23 AM
Hi,
I'm in some trouble again, and I bet someone of you has got the solution ;o)

I built a class that keeps track of the existing instances of the class. I decided to have a static class member of type std::map the key should be some integer-value identifying the actual instance, the value should be a pointer to the object itself.

The class looks like this:

class MyClass
{
private:
map<int,MyClass*> m_ClassMap;


public:
MyClass()

..
..

}


So far, so good. In the Constructor each of the instances shall register itself to the classmap. Therefore I put this inside the constructor:

m_ClassMap[inst] = this;

where inst is a value of type integer, for identifying a particular instance later.

Ok,when I compile it I get a linker-error telling me, that there is an unresolved external MyClass::m_ClassMap. Does anybody know why ? I don't...

To describe why I do the things explained above: Because of Win32-Architecture I have to build one function of my class as static to avoid global functions. Win32API wants a function-pointer and is not very amused about class-methods because of the implicit this-pointer they have.

For that reason I decided to make one static function for handling all. This function gets the Id of the actual instance and looks up this id in the map described above, gets the associated instance-pointer and calls a function upon it.

I think this is the easiest way to handle with this case, anyway I would appreciate any suggestions to get this done in a better way.

But for now: Why do I get this Linkererror ?

Thanx in advance

Juergen

dimm_coder
April 25th, 2003, 06:47 AM
If u have a static member

for ex. say:

foo.h
class foo
{
static int a;

};

U need to write somewhere in foo.cpp

static foo::int a = 0;

So U need to write in cpp-file

static CMyClass::map<...>m_maplist;

Got it?

:)

AlionSolutions
April 25th, 2003, 07:46 AM
the compiler gives me a redeclaration-error when I do this

I remember something like the syntax you used from the last time I had a nearly simmilar problem, but I can't remember :o(

dimm_coder
April 25th, 2003, 08:04 AM
Show both h and cpp files with a code related to your class and this problem.

Gabriel Fleseriu
April 25th, 2003, 08:06 AM
This will compile (if you define inst):

class MyClass
{
public:
MyClass()
{
m_ClassMap[inst]=this;
}
private:
static std::map<int,MyClass*> m_ClassMap;
};

std::map<int,MyClass*> MyClass::m_ClassMap;

Paul McKenzie
April 25th, 2003, 08:08 AM
Originally posted by AlionSolutions
The class looks like this:

class MyClass
{
private:
map<int,MyClass*> m_ClassMap;


public:
MyClass()

..
..

}
Funny, I don't see any static members declared in your code. If you have an error, please post the exact code that is in your class. It just leads to confusion if you don't post your actual code.So far, so good. In the Constructor each of the instances shall register itself to the classmap. Therefore I put this inside the constructor:

m_ClassMap[inst] = this;

where inst is a value of type integer, for identifying a particular instance later.

Ok,when I compile it I get a linker-error telling me, that there is an unresolved external MyClass::m_ClassMap. Does anybody know why ? I don't...
With the code you posted, there should be no such error. If you meant that your class looks like this:

class MyClass
{
private:
static map<int,MyClass*> m_ClassMap;

};

Then you must also define the static member. This is the rule of C++ and has nothing to do with map. Static members of classes must be defined in one of your modules:

// In one of your CPP files, and outside of any functions:
map<int, MyClass*> MyClass::m_ClassMap;

Also, you should use a typedef for your map. You'll go nuts typing in map<int, MyClass*> everywhere in your code.

class MyClass;
typedef std::map<int, MyClass*> MyClassMap;

class MyClass
{
private:
static MyClassMap m_ClassMap;
};

Regards,

Paul McKenzie

dimm_coder
April 25th, 2003, 08:21 AM
Originally posted by dimm_coder
If u have a static member

U need to write somewhere in foo.cpp
static foo::int a = 0;
So U need to write in cpp-file
static CMyClass::map<...>m_maplist;
Got it?

:)

Sorry, It was the fade of my mind //
of course
static int foo::a = 0;
and
static map<...> CMyClass::m_maplist;

Sorry.

AlionSolutions
April 25th, 2003, 08:22 AM
As many times I lost my brain, you helped me again to regain parts of it ;o)

THNX