473,503 Members | 1,864 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<typename 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 3157
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<typename 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<typename 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<typename 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>::NB << 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
2382
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...
16
2952
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
12439
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...
9
6347
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...
3
5692
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...
4
3090
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,...
5
3585
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...
3
5777
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
8352
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...
4
5802
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...
0
7093
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...
0
7287
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
7348
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...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5021
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...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
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 ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.