By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

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

P: n/a


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: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Apr 5 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Leslaw Bieniasz wrote:
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

Apr 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.