Connecting Tech Pros Worldwide Forums | Help | Site Map

static const class members

Jan13
Guest
 
Posts: n/a
#1: Jul 19 '05

Hi,



I'm new to programming in C++ (using VC6) and ran into the following
problem: I want to declare and define a class member variable as 'static
const', but something seems to go wrong with the linking.



I specify a class Port the following way:

Port.h:

class __declspec(dllexport) Port

{ static const int IN_PORT;

//...

}



Port.cpp:

#include "Port.h"

const int Port::IN_PORT=0;

//...



When I build my project containing this code, it's all ok. I can use
this class as expected within the project.

However, when I want to use this variable from another project and class
(note that the Port-class is in a dll), I get a linker error:

AddInt.obj : error LNK2001: unresolved external symbol "public: static
int const Port::TYPE_CObject" (?TYPE_CObject@Port@@2HB)



I don't know excatly why this is a problem: I imported the Port.h-file
and the whole Port-class was exported from the dll using
__declspec(dllexport). Maybe dll's and static class members need some
sort of special treatment? Maybe, since the source only includes Port.h,
I need to initialize the constant in Port.h?

I wasn't sure about this, but tried to do this and ran into another
problem. I used the following code and got the following error:



Port.h:

class __declspec(dllexport) Port

{ static const int IN_PORT=0;

//...

}



Port.cpp:

#include "Port.h"

//const int Port::IN_PORT=0;

//...



d:\programming\c++\luctor\src\pipeline\port.h(13) : error C2252:
'IN_PORT' : pure specifier can only be specified for functions



Apparently, VC assumes my beautiful IN_PORT variable is/wants to be a
virtual function, because I define it as =0... Does anyone know why it
assumes this? I have never used the 'virtual' keyword in my short, but
exciting C++ career, so I have no clue why it starts whining about pure
virtual function specifiers.



Can anybody help me with this problem (actually, I have 2 problems:
the usage of the static member from a dll and the initialisation of
the static)?



I won't post my complete source, because that's rather large, but I can
mail it if anyone wants to have a look at it. Thanks in advance,



Jan


--
Posted via http://dbforums.com

stephan beal
Guest
 
Posts: n/a
#2: Jul 19 '05

re: static const class members


Jan13 wrote:[color=blue]
> class __declspec(dllexport) Port
>
> { static const int IN_PORT;[/color]

AFAIK a const must be defined when it is declared:

static const int IN_PORT = 0;

but only integral types can be initialized that way, i believe.

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

WW
Guest
 
Posts: n/a
#3: Jul 19 '05

re: static const class members


Jan13 wrote:[color=blue]
> I specify a class Port the following way:
>
> Port.h:
>
> class __declspec(dllexport) Port
>
> { static const int IN_PORT;
>
> }
>
> Port.cpp:
>
> #include "Port.h"
>
> const int Port::IN_PORT=0;
>
> //...
>
>
>
> When I build my project containing this code, it's all ok. I can use
> this class as expected within the project.
>
> However, when I want to use this variable from another project and
> class (note that the Port-class is in a dll), I get a linker error:
>
> AddInt.obj : error LNK2001: unresolved external symbol "public: static
> int const Port::TYPE_CObject" (?TYPE_CObject@Port@@2HB)[/color]

While this is completely off-topic, I will give my guess:

__declspec(dllexport) const int Port::IN_PORT=0;

You need to export the variable itself, because its name does not seem
to be exported.
[color=blue]
> I wasn't sure about this, but tried to do this and ran into another
> problem. I used the following code and got the following error:
>
> Port.h:
>
> class __declspec(dllexport) Port
>
> { static const int IN_PORT=0;[/color]

This is an error in VC6. You need to say:

enum {
IN_PORT=0
};

White Wolf


jeffc
Guest
 
Posts: n/a
#4: Jul 19 '05

re: static const class members



"Jan13" <member44596@dbforums.com> wrote in message
news:3492831.1066388874@dbforums.com...[color=blue]
>
> Apparently, VC assumes my beautiful IN_PORT variable is/wants to be a
> virtual function, because I define it as =0... Does anyone know why it
> assumes this? I have never used the 'virtual' keyword in my short, but
> exciting C++ career, so I have no clue why it starts whining about pure
> virtual function specifiers.[/color]

That's just a manifestation of the same problem. You have a VC++ linkage
problem, not a C++ problem. Try the microsoft.public.vc.* newsgroups - they
will know better.


Closed Thread