template<class T>
class SimNode{
T data;
int link;
public:
friendclass SimSpace;
friendclass 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)
returnfalse;
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;
returntrue;
}
returnfalse;
}
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.
//forward declare
template<class T> class SimSpace;
template<class T> class SimChain;
template<class T>
class SimNode{
friendclass SimSpace<T>;
friendclass SimChain<T>; //they are templates
//...
template<class S>
class SimSpace{
friendclass 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).