Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

map as a class static member

7 views
Skip to first unread message

Marina

unread,
Jan 15, 2003, 10:24:45 AM1/15/03
to
Hi,
I'm trying to use a map container as a static member of my
class and I'm getting linking error: "unresolved external
symbol" for my map class member.

Is there some constrain for making 'map' static or do I
miss something? Ideally I would like to have a singelton
class with a map container.

Actually, I don't have this problem if I define my static
as a pointer to map instead of just map and I do
initialization of this pointer to NULL in the source file.
Maybe I should do any kind of initialization of the static
map but I don't know how to do it.

thanks in advance,
Marina

Carl Daniel [MVP]

unread,
Jan 15, 2003, 10:43:48 AM1/15/03
to

"Marina" <marin...@yahoo.com> wrote in message
news:69d901c2bcaa$3bb9cdd0$8df82ecf@TK2MSFTNGXA02...

When you declare a static member of a class, you also need to define it
somewhere, e.g.:

// MyClass.h

#include <map>

class MyClass
{
static std::map<int,int> sm_map;

// other members of MyClass.
};


// MyClass.cpp

#include "MyClass.h"

std::map<int,int> Myclass::sm_map; // definition for static member.

-cd

Marina

unread,
Feb 10, 2003, 2:58:26 PM2/10/03
to
My class is a part of the DLL interface. And when I define
a static member in the source file as recommended:

#include "MyClass.h"
std::map<int,int> Myclass::sm_map; // definition for
static member.

I'm getting the following compilation error:

"Myclass::sm_map : definition of dllimport static data
member not allowed"

What can I do in such situation?

>.
>

Andy

unread,
Feb 11, 2003, 11:25:13 AM2/11/03
to
"Marina" <marin...@yahoo.com> wrote in message
news:08a901c2d13e$c696bbe0$a001...@phx.gbl...

> My class is a part of the DLL interface. And when I define
> a static member in the source file as recommended:
>
> #include "MyClass.h"
> std::map<int,int> Myclass::sm_map; // definition for
> static member.
>
> I'm getting the following compilation error:
>
> "Myclass::sm_map : definition of dllimport static data
> member not allowed"
>
> What can I do in such situation?

My instinct would be to put your map in another class that you are not
exporting and provide and interface to it through your exported class.

Think pimpl.

HTH

Andy.

Andy

unread,
Feb 13, 2003, 6:39:00 AM2/13/03
to

"Andy" <arb7...@hotmail.com> wrote in message
news:3e492393$0$5925$afc3...@news.easynet.co.uk...

> "Marina" <marin...@yahoo.com> wrote in message
> news:08a901c2d13e$c696bbe0$a001...@phx.gbl...
> > My class is a part of the DLL interface. And when I define
> > a static member in the source file as recommended:
> >
> > #include "MyClass.h"
> > std::map<int,int> Myclass::sm_map; // definition for
> > static member.
> >
> > I'm getting the following compilation error:
> >
> > "Myclass::sm_map : definition of dllimport static data
> > member not allowed"
> >
> > What can I do in such situation?
>
> My instinct would be to put your map in another class that you are not
> exporting and provide and interface to it through your exported class.
>

I have done some testing and discovered that this is not necessary.
I was able to use and exported a class from a dll with static member no
problem.

If you are interested I can send the code - I am using it on Visual
Studio.Net - but have also tested on v6.
Let me know which version you are using.

I am assuming that your exported static is private - and clients access it
via public member functions of you class?
Can you give some more detail - were you compiling the dll or the client exe
when you got this error ?

> Think pimpl.

Although not strictly necessary to solve you problem - not a bad idea in any
case.

0 new messages