Static const variables cause linker problems under Qt

R

Rune Allnor

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
 
V

Victor Bazarov

Rune said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top