static const class members

J

Jan13

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
 
W

WW

Jan13 said:
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)

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

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

enum {
IN_PORT=0
};

White Wolf
 
S

stephan beal

Jan13 said:
class __declspec(dllexport) Port

{ static const int IN_PORT;

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

jeffc

Jan13 said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top