By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,671 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,671 IT Pros & Developers. It's quick & easy.

static variables in template class

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

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

P: n/a
thanks a lot!

Shahin
Jun 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.