how to define/initialise static template class data members of classtype

  • Thread starter Leslaw Bieniasz
  • Start date
L

Leslaw Bieniasz

Hello,

I have the following problem:

file a.h ---------------

template <class T>
class A
{
// some stuff
};


template class A<float>;
template class A<double>;


file b.h ---------------


template <class T>
class B
{
// some stuff

private:
static A<T> A_static;
};

// ???

template class B<float>;
template class B<double>;


--------------------------

My question is:

according to the rules of C++, static data member A_static of B
has to be additionally defined (and initialised) outside the class
declaration of B, otherwise we get linker errors (missing external).
What exactly should I do, and where should I put such a definition of
A_static? I note that template class A does not actually require any
initialisation, because operations on A_static are supposed
to take place later during the program run.
My attempt to place the statement:

template <class T> A<T> B<T>::A_static;

in the place indicated by three question marks produces
linker warnings stating that variable A_static is defined
twice; once in the module for B, and once in the module that calls
b.h (which is a quite understandable, but unwanted effect).
The problem does not seem to arise if A_static is not a class
instance, but some built-in type, like, for example

static int A_static;

Then a statement

template <class T> int B<T>::A_static = 1;

placed in the line with three question marks works fine.

If this plays any role, I am using Borland C++ Builder 6.0.

L.B.


*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
M

mlimber

Leslaw said:
Hello,

I have the following problem:

file a.h ---------------

template <class T>
class A
{
// some stuff
};


template class A<float>;
template class A<double>;


file b.h ---------------


template <class T>
class B
{
// some stuff

private:
static A<T> A_static;
};

// ???

template class B<float>;
template class B<double>;


--------------------------

My question is:

according to the rules of C++, static data member A_static of B
has to be additionally defined (and initialised) outside the class
declaration of B, otherwise we get linker errors (missing external).
What exactly should I do, and where should I put such a definition of
A_static? I note that template class A does not actually require any
initialisation, because operations on A_static are supposed
to take place later during the program run.
My attempt to place the statement:

template <class T> A<T> B<T>::A_static;

in the place indicated by three question marks produces
linker warnings stating that variable A_static is defined
twice; once in the module for B, and once in the module that calls
b.h (which is a quite understandable, but unwanted effect).
The problem does not seem to arise if A_static is not a class
instance, but some built-in type, like, for example

static int A_static;

Then a statement

template <class T> int B<T>::A_static = 1;

placed in the line with three question marks works fine.

If this plays any role, I am using Borland C++ Builder 6.0.

Your stated solution is the correct one and works fine for me on VC++
6.0 (sp6). Better check on a Borland-specific newsgroup or with
Borland's tech support.

Cheers! --M
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top