473,569 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static variables in template class

Hi,

I'd like to put a static member in a template class, but I don't know
the syntax. I've tried out

template<typena me Type> class Stocker : private vector<Type>
{
public:

static int NB;
};

int Stocker<double> ::NB(5);
int main()
{

cout << Stocker::NB << endl;
return 0;

}

but it doesn't work, it gives me

error: too few template-parameter-lists

could someone please help me out?

thanks a lot!

Shahin
Jun 9 '06 #1
3 3163
Shahin Tavakoli wrote:
I'd like to put a static member in a template class, but I don't know
the syntax. I've tried out

template<typena me Type> class Stocker : private vector<Type>
{
public:

static int NB;
};

int Stocker<double> ::NB(5);

int main()
{

cout << Stocker::NB << endl;
Here 'Stocker' is a template-id. You cannot use it here without its
template arguments:

cout << Stocker<double> ::NB << endl;


return 0;

}

but it doesn't work, it gives me

error: too few template-parameter-lists

could someone please help me out?


See above.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 9 '06 #2

Shahin Tavakoli wrote:
Hi,

I'd like to put a static member in a template class, but I don't know
the syntax. I've tried out

template<typena me Type> class Stocker : private vector<Type>
{
public:

static int NB;
};

int Stocker<double> ::NB(5);
int main()
{

cout << Stocker::NB << endl;
return 0;

}

but it doesn't work, it gives me

error: too few template-parameter-lists

could someone please help me out?

thanks a lot!

Shahin


1>You need to define the static member of the template class

2>If you need a "special" definition for double, then specialize the
member variable.

template<typena me Type> class Stocker : private vector<Type>
{
public:
static int NB;
};

template <typename T>
int Stocker<T>::NB;

template<>
int Stocker<double> ::NB(5);

int main()
{
cout << Stocker<int>::N B << endl;
cout << Stocker<double> ::NB << endl;
return 0;
}

Jun 9 '06 #3
thanks a lot!

Shahin
Jun 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2394
by: Nils | last post by:
Hi, Ich have a template class and want to initialize a static variable, that is available for all objekts of the template class, doesn't matter what template parameter is used. Can someone please tell me how it will work! Thanks a lot! Nils
16
2957
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
7
12445
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this data will be initialized if it is used across many translation unit, assume that it has constructor and needs dynamic initialization? I have...
9
6350
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you...
3
5697
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I have a base class with several sub classes. In runtime I want to create a instance of a sub class and assign it to a base class pointer. Nothing...
4
3095
by: mnowosad | last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time an executing code within an AppDomain references a class for the the first time since the AppDomain was created/loaded, the .NET executes the assignments done in the class static variables declarations and runs the static constructor of that class. So, I expected...
5
3594
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 ...
3
5781
by: StephQ | last post by:
In utility.hpp I have: namespace utility { template <class T, double (T::*F)(double) const> class Display { private: static double resolution;
17
8360
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to...
4
5806
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public Element_common<L2, Side<2,2 { public:
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
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...
0
7924
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. ...
0
8125
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...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3653
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...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
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
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.