算法出错,求教
这是模拟指针的公式化描述,调试时总出错
#include<iostream>
using namespace std;
template<typename T>
class SimNode
{
public:
friend class SimSpace<T>;
private:
T data ;
int link;
};
template<typename T>
class SimSpace
{
public:
SimSpace(int MaxSpaceSize = 100) ;
~SimSpace() {delete[] node;}
int Allocate() ;
void Deallocate(int& i);
private:
int NumberOfNodes, first ;
SimNode<T> *node;
};
template<typename T>
SimSpace<T>::SimSpace(int MaxSpaceSize)
{
NumberOfNodes = MaxSpaceSize ;
node = new SimNode<T>[NumberOfNodes] ;
for(int i=0; i<NumberOfNodes-1; i++)
node[i].link = i+1 ;
node[NumberOfNodes-1].link=-1;
first = 0 ;
}
template<typename T>
int SimSpace<T>::Allocate()
{
if(first=-1) throw NoMem() ;
int i =first ;
first = node[i].link;
return i;
}
template<typename T>
void SimSpace<T>::Deallocate(int& i)
{
node[i].link =first ;
first = i ;
i = -1 ;
}
int main()
{
return 0 ;
}
错误为:
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(8) : error C2059: syntax error : '<'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(12) : see reference to class template instantiation 'SimNode<T>' being compiled
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(8) : error C2238: unexpected token(s) preceding ';'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(12) : see reference to class template instantiation 'SimNode<T>' being compiled
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(25) : error C2989: 'SimSpace' : template class has already been defined as a non-template class
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(8) : see declaration of 'SimSpace'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(28) : error C2059: syntax error : '<'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(39) : error C2039: 'Allocate' : is not a member of '`global namespace''
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(40) : error C2143: syntax error : missing ';' before '{'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(40) : error C2447: missing function header (old-style formal list?)
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(47) : error C2954: template definitions cannot nest
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(48) : error C2143: syntax error : missing ';' before '<'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(48) : error C2182: 'SimSpace' : illegal use of type 'void'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(48) : error C2059: syntax error : ';'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(48) : error C2143: syntax error : missing ';' before '<'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(48) : error C2039: 'Deallocate' : is not a member of '`global namespace''
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(56) : error C2143: syntax error : missing ';' before '{'
C:\Users\SuSu\Desktop\C++\SimSpace\SimSpace.cpp(56) : error C2447: missing function header (old-style formal list?)
执行 cl.exe 时出错.
SimSpace.exe - 1 error(s), 0 warning(s)
请教到底是哪里错了?
[解决办法]
template<class T> // 没有发现这里多了一段类模板申明吗?class SimSpace;template<typename T>class SimNode{public: friend class SimSpace<T>;private: T data ; int link;};