template Error

Hi

this code does not work
Where can I have done wrong


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

 template<class T>
class SimNode{	
	T data;
	int link;
public:
	friend class SimSpace;
	friend class SimChain;

};

template<class S>
class SimSpace{
	int NumberOfNodes;
	int first;
	SimNode<S> *node;
public:
	SimSpace(int MaxSpaceSize=100)
	{
	
		NumberOfNodes = MaxSpaceSize;
		node = new SimNode[NumberOfNodes];
		for(int i=0; i<NumberOfNodes-1; i++)
			node[i].link = i+1;
		node[NumberOfNodes-1].link = -1;
		first = 0;
	}
	~SimSpace()
	{
		delete[]node;
	}
	int Allocate()
	{
		if(first == -1)
			throw out_of_range("Error");
			int i= first;
		first = node[i].link;
		return i;
	}
	void Deallocate(int& i)
	{
		node[i].link = first;
		first = i;
		i= -1;
	}
	//friend class SimChain;

	

};






////////////////////////////////////////////////////////////////////////////////
template<class TT>
class SimChain{
	TT first;
	static SimSpace<TT> S;
public:
	SimChain()
	{
		first= -1;
	}
	~SimChain()
	{
		Destroy();
	}
	void Destroy()
	{
		int next;
		while(first!=-1) {
			next = S.node[first].link;
			S.Deallocate(first);
			first = next;
		}
	}

	int Length()const
	{
		int current = first;
		int len =0;
		while(current!= -1) {
		     current = Snode[current].link;
		     len++;
	    }
		return len;
	}

	bool Find(int k, TT &x)const
	{
		if(k<1) 
			return false;
		int current= first;
		int index= 1;
		while(index < k && current!= -1) {
			current= S.node[current].link;
			index++;
		}
		if(current != -1){
			x = S.node[current].data;
			return true;
		}
		return false;
	}


	SimChain<T> &Delete(int k, TT &x)
	{
		if(k<1 || first == -1)
			throw out_of_range("Error");
		int p = first;

		if(k == 1)
			first = S.node[first].link;
		else{
			int q=first;
			for(int index=1 ; index< k-1 && q!=-1;index++)
				q= S.node[q].link;
			if(q== -1 ||S.node[q].link == -1 )
				throw out_of_range("Error");
			p= S.node[q].link;

			S.node[q].link = S.node[p].link;
		}

		x= S.node[p].data;
		S.Deallocate(p);
		return *this;
	}
	SimChain<TT> &Insert(int k,const TT &x)
	{
		
		if(k < 0) 
			throw out_of_range("Error");
		int p = first;
		for(int index=1; index < k && p!= -1 ; index++)
			p= S.node[p].link;

		if(k > 0 && p==-1)
			throw out_of_range("Error");
		int y = S.Allocate();
		S.node[y].data = x;

		if(k){
			S.node[y].link = S.node[p].link;
			S.node[p].link = y;
		}
		else{
			S.node[y].link = first;
			first = y;
		}

		return *this;
	}
	//void Output(ostream& out)const;
	


};
 


Output of the program as follows:


1>main.cpp(3334): error C2989: 'SimSpace' : class template has already been declared as a non-class template
1>          main.cpp(3291) : see declaration of 'SimSpace'
1>main.cpp(3296): error C3857: 'SimSpace': multiple template parameter lists are not allowed
1>main.cpp(3446): error C2989: 'SimChain' : class template has already been declared as a non-class template
1>          main.cpp(3292) : see declaration of 'SimChain'
1>main.cpp(3342): error C3857: 'SimChain': multiple template parameter lists are not allowed
1>main.cpp(3458): error C2143: syntax error : missing ';' before '<'
1>main.cpp(3458): error C2143: syntax error : missing ';' before '<'
1>
1>Build FAILED.


I'm not sure about this:
1
2
3
4
 template<class T>
class SimNode{	
//friend class SimSpace;
friend class SimSpace<T>; //SimSpace is a template 


1
2
template<class TT>
SimChain<T> &Delete(int k, TT &x)
T doesn't exist

Could you point the lines that are giving you errors?
Last edited on
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
template<class T>
class SimNode{	
	T data;
	int link;
public:
	friend class SimSpace;
	friend class SimChain;

};

template<class S>    //: error C3857: 'SimSpace': multiple template parameter lists are not allowed
class SimSpace{
	int NumberOfNodes;
	int first;
	SimNode<S> *node;
public:
	SimSpace(int MaxSpaceSize=100)
	{
	
		NumberOfNodes = MaxSpaceSize;
		node = new SimNode[NumberOfNodes];
		for(int i=0; i<NumberOfNodes-1; i++)
			node[i].link = i+1;
		node[NumberOfNodes-1].link = -1;
		first = 0;
	}
	~SimSpace()
	{
		delete[]node;
	}
	int Allocate()
	{
		if(first == -1)
			throw out_of_range("Error");
			int i= first;
		first = node[i].link;
		return i;
	}
	void Deallocate(int& i)
	{
		node[i].link = first;
		first = i;
		i= -1;
	}
	//friend class SimChain;

	

};






////////////////////////////////////////////////////////////////////////////////
template<class TT>  // error C3857: 'SimChain': multiple template parameter lists are not allowed
class SimChain{
	TT first;
	static SimSpace<TT> S;
public:
	SimChain()
	{
		first= -1;
	}
	~SimChain()
	{
		Destroy();
	}
	void Destroy()
	{
		int next;
		while(first!=-1) {
			next = S.node[first].link;
			S.Deallocate(first);
			first = next;
		}
	}

	int Length()const
	{
		int current = first;
		int len =0;
		while(current!= -1) {
		     current = Snode[current].link;
		     len++;
	    }
		return len;
	}

	bool Find(int k, TT &x)const
	{
		if(k<1) 
			return false;
		int current= first;
		int index= 1;
		while(index < k && current!= -1) {
			current= S.node[current].link;
			index++;
		}
		if(current != -1){
			x = S.node[current].data;
			return true;
		}
		return false;
	}


	SimChain<T> &Delete(int k, TT &x)
	{
		if(k<1 || first == -1)
			throw out_of_range("Error");
		int p = first;

		if(k == 1)
			first = S.node[first].link;
		else{
			int q=first;
			for(int index=1 ; index< k-1 && q!=-1;index++)
				q= S.node[q].link;
			if(q== -1 ||S.node[q].link == -1 )
				throw out_of_range("Error");
			p= S.node[q].link;

			S.node[q].link = S.node[p].link;
		}

		x= S.node[p].data;
		S.Deallocate(p);
		return *this;
	}
	SimChain<TT> &Insert(int k,const TT &x)
	{
		
		if(k < 0) 
			throw out_of_range("Error");
		int p = first;
		for(int index=1; index < k && p!= -1 ; index++)
			p= S.node[p].link;

		if(k > 0 && p==-1)
			throw out_of_range("Error");
		int y = S.Allocate();
		S.node[y].data = x;

		if(k){
			S.node[y].link = S.node[p].link;
			S.node[p].link = y;
		}
		else{
			S.node[y].link = first;
			first = y;
		}

		return *this;
	}
	//void Output(ostream& out)const;
	


}; //error: 'SimSpace' : class template has already been declared as a non-class template



int main()
{
	
	
	SimChain<int> c;   // error C2143: syntax error : missing ';' before '<',   error C2143: syntax error : missing ';' before '<'


	return 0;
}

 



Changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//forward declare
template<class T> class SimSpace;
template<class T> class SimChain;

template<class T>	
class SimNode{
	friend class SimSpace<T>;
	friend class SimChain<T>; //they are templates
//...
template<class S>
class SimSpace{
	friend class SimChain<S>; //you try to access to private members in SimChain::Destroy

	SimSpace(int MaxSpaceSize=100)
	{
		NumberOfNodes = MaxSpaceSize;
		node = new SimNode<S>[NumberOfNodes]; //SimNode is a template 
I'm getting trouble with the static declaration.

by the way, you don't need to change the template alias for every class (in fact, is confusing).
ridiculous mistakes....
Thanks man.

Why did you do it ?

template<class T> class SimSpace;
template<class T> class SimChain;
Last edited on
Topic archived. No new replies allowed.