how to declare a private static variable?

Hello guys. I am making a single linked list and I am stuck at a point where i have to declare a dummy head node as static. Problem is how can I initialize this variable now?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <typename type>
class SnglLnkLstDmHdr
{
public:
	SnglLnkLstDmHdr()
	{
		this->Start = nullptr;
	}
private:
	struct Node
	{
		type Data;
		Node *link;
	};

	Node *Start;
	static Node *DummyHead;
};
1
2
template <typename type>
SngLnkLstDmHdr<type>::Node* SngLnkLstDmHdr<type>::DummyHead = /*stuff*/ ;

This would need to be placed in the same header file since the class is a templated class.
Last edited on
I tried this earlier but this won't do any good.
Can you be more specific? What does "won't do any good" means?
Compile error? What error?
Not working properly? What did you expect and what you get?

This should do the job:
1
2
template <typename type>
typename SnglLnkLstDmHdr<type>::Node* SnglLnkLstDmHdr<type>::DummyHead = nullptr;
this gives error... The program dont even gets compiled...
this gives error...
What error?
What "this"? My code, previous code, someone else code?
Did you place everything in same header?
Basically I'm not doing this in header.. I am writing this class above my main function.. Iget following errors:

syntax error : missing ';' before '*'
error C2065: 'type' : undeclared identifier
'SnglLnkLstDmHdr' : 'type' is not a valid template type argument for parameter 'type'
And what is your code?
Because this is working fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
template <typename type>
class SnglLnkLstDmHdr
{
public:
	SnglLnkLstDmHdr()
	{
		this->Start = nullptr;
	}
private:
	struct Node
	{
		type Data;
		Node *link;
	};

	Node *Start;
	static Node *DummyHead;
};

template <typename type>
typename SnglLnkLstDmHdr<type>::Node* SnglLnkLstDmHdr<type>::DummyHead = nullptr;

int main()
{
    SnglLnkLstDmHdr<int> f;
}
http://ideone.com/8cqIvA
http://coliru.stacked-crooked.com/a/aa6361d7c5461d50
http://melpon.org/wandbox/permlink/uhKUrgHt7W5QgQBd
http://cpp.sh/3o5
Mysteriously your code is working and mine is giving error.. Although they are exactly same but mine code is giving error.. I guess it's a compiler issue.. Thanks alot.. :)
Topic archived. No new replies allowed.