user9196120
user9196120

Reputation: 401

How to define a static member of a template class?

I have the following declaration inside my template class declaration:

template<typename T1>
class node{

public:
static const shared_ptr<node<T1>> null_sp2node;

};

I am not sure how I could define this static const member?

I tried

template<typename T1> static const shared_ptr<node<T1>>
node<T1>::null_sp2node = NULL;

But get the following compiler error:

error: 'static' can only be specified inside the class definition

What is the correct way of defining the static member in this instance?

Upvotes: 1

Views: 66

Answers (1)

jfMR
jfMR

Reputation: 24738

As already said by the error message, remove the static keyoword when defining (as opposed to declaring) the static member null_sp2node:

template<typename T1> const shared_ptr<node<T1>>
node<T1>::null_sp2node = NULL;

The keyword static should be only used when declaring the static member inside the class definition (just as you did in the node class template's definition), but not when defining the member (i.e.: creating the storage for it).

Upvotes: 1

Related Questions