473,772 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static const class members


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(dlle xport) 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_CObj ect" (?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(dlle xport). 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(dlle xport) 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
Jul 19 '05 #1
3 17639
Jan13 wrote:
class __declspec(dlle xport) 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.

Jul 19 '05 #2
WW
Jan13 wrote:
I specify a class Port the following way:

Port.h:

class __declspec(dlle xport) 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_CObj ect" (?TYPE_CObject@ Port@@2HB)
While this is completely off-topic, I will give my guess:

__declspec(dlle xport) 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(dlle xport) Port

{ static const int IN_PORT=0;


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

enum {
IN_PORT=0
};

White Wolf
Jul 19 '05 #3

"Jan13" <me*********@db forums.com> wrote in message
news:34******** ********@dbforu ms.com...

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.publi c.vc.* newsgroups - they
will know better.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3614
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a DLL by another external object file (not within the DLL). It only occurs when the static const...
3
2238
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
2
1977
by: John Stiles | last post by:
I have written some pretty simple code which works great in Dev Studio and CodeWarrior, but gcc is giving me link errors. Here is what I've been able to distill it down to: namespace Private { template <class T> struct lengthof; template <class T, size_t N> struct lengthof<T> { static const size_t array_size = N; }; }
2
3590
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double ProtonMassInAtomicUnits = 1836.152755656068; } I try to use these in another header to initialize static const member
2
7501
by: Daniel Switkin | last post by:
Hi there, I'm trying to do the following: class tLimits { static const int kIntMin = 0; // fine static const float kFloatMin = 0.0f; // breaks }; and I get this message:
5
2125
by: Miguel Guedes | last post by:
Hello, Why can't non-integral static const data members be initialized within a class? I know how to initialize the members but I don't understand the underlying reason to this rule. Would someone explain this to me? Example: class foo
3
2670
by: Alexander Hans | last post by:
Hi, what's the usual way in C++ to define a static constant class member? Consider the following example: class TestClass { public: static const double a = 12.5; static const double b = 2 * a;
12
6253
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is: gcc version 4.0.1 (Apple Inc. build 5465). thanks for the help. summary of compile error: --------------------------------------- cpp.C:4: error: non-static const member 'const unsigned int A::u',
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.