static variables in template class

  • Thread starter Shahin Tavakoli
  • Start date
S

Shahin Tavakoli

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
 
V

Victor Bazarov

Shahin said:
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 said:
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
 
A

amparikh

Shahin said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,249
Latest member
KattieCort

Latest Threads

Top