473,795 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 Electrochemistr y? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Apr 5 '06 #1
1 2566
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
4618
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
7
3590
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 you cannot define a static data member of a template class if it ONLY has a default-initializer...
1
2356
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
7735
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. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For example: class B { protected:
7
2597
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
3610
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 fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says core/profile.cc:199: error: `kNumBins' was not declared in this scope
5
2576
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: const char *s = string_mapper<thetype>(); However I do not know the string to be associated with the type at compile time, and will need a way to set up the mapping, to be created at run time, possibly like so: void foo(char*...
8
1867
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 DATA2_H class Data2{ public:
5
7242
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
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.