473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2533
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
7
by: Paul | last post by:
Hi, I have a problem with the C++ spec 14.7.3.15 (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14761 for relevant discussion). It seems that the code (below) is impossible to get right, as...
1
by: Old Wolf | last post by:
I tried this code: #include <iostream> #include <string> template<typename T> struct enum_properties { static const long max; static const std::string name;
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
7
by: sunil | last post by:
Hi, I am dealing with problem where I need virtual+static function: enum {A=0,B=1}; Factory.cpp: ------------------ Base * createInstance(int classType,char *name) { if(classType == A)
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
8
by: zl2k | last post by:
hi, there Here is a simplified piece of code of my program, it compiles and runs fine. However, valgrind shows it has uninitialized problem. What I am doing wrong? #ifndef DATA2_H #define...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.