Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Static const variables cause linker problems under Qt

Reply
Thread Tools

Static const variables cause linker problems under Qt

 
 
Rune Allnor
Guest
Posts: n/a
 
      02-05-2010
Hi all.

I have a template class where I have defined some
constants in a header file, along the lines of

////////////////////////////////////////////
class flags{
static const size_t flag;
};

const size_t flags::flag = 1;
////////////////////////////////////////////

This compiles and runs nicely in the Boost.Test program,
but when I try to use the template class in a Qt GUI program,
the linker starts complaining about the constants as being
multiple defined objects:

- in main.obj
- in moc_myfile.obj
- in myfile.obj

I have included the standard guard about multiple inclucions
of the header files, and I can see why these constants would
appear in myfile.obj, as I include the file where they are declared
from myfile.h. However, I have no idea why the constants should
be available in main.obj or moc-myfile.obj - that's where Qt come
in to do its thing.

There isn't much I can do about the ways Qt twists and turns
the code into an executable, so I suppose my only hope
is to be able to resolve the 'multiple definition' problem.

Any hints on how to do this?

The flags class will exist in millions of instances, so I can not
afford to include the flag definintions as const variables in the
class.

Rune
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-05-2010
Rune Allnor wrote:
> I have a template class where I have defined some
> constants in a header file,


*Never* define your static data that you might want to link to, in a
header. *Always* define them in *one of the source files*. As soon as
you do that, your linker problems will disappear.

Const or no const plays no role here.

> along the lines of
> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static const T vs static T const er C++ 3 04-22-2008 10:22 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM
Linker error with static const field Martin Magnusson C++ 2 10-20-2003 09:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57