471,049 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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



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
1 2371
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.

Similar topics

1 post views Thread by Old Wolf | last post: by
13 posts views Thread by Adam H. Peterson | last post: by
7 posts views Thread by sunil | last post: by
5 posts views Thread by mast2as | last post: by
8 posts views Thread by zl2k | last post: by

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.