473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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(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
Jul 19 '05 #1
3 17612
Jan13 wrote:
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.

Jul 19 '05 #2
WW
Jan13 wrote:
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
Jul 19 '05 #3

"Jan13" <me*********@dbforums.com> wrote in message
news:34****************@dbforums.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.public.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
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...
3
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. ...
2
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 {...
2
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...
2
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
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...
3
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...
12
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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...

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.