编译时头文件出错,求大家知道怎么改?(数据结构顺序表头文件,源文件没问题,)最怕头文件出错,蛋疼!
错误提示:
--------------------Configuration: data structures - Win32 Debug--------------------
Compiling...
data1.cpp
c:\program files\microsoft visual studio\data structures\seqlist.h(45) : error C2059: syntax error : ''template<''
c:\program files\microsoft visual studio\data structures\seqlist.h(46) : error C2065: 'T' : undeclared identifier
c:\program files\microsoft visual studio\data structures\seqlist.h(56) : error C2904: 'T' : template-name already defined as 'int T'
c:\program files\microsoft visual studio\data structures\seqlist.h(58) : error C2143: syntax error : missing ';' before '{'
c:\program files\microsoft visual studio\data structures\seqlist.h(58) : error C2447: missing function header (old-style formal list?)
c:\program files\microsoft visual studio\data structures\seqlist.h(64) : error C2954: template definitions cannot nest
执行 cl.exe 时出错.
data1.obj - 1 error(s), 0 warning(s)
源代码:
#include"linearlist.h"
template <class T>
class SeqList:public LinearList<T>
{
public:
SeqList(int MaxListSize);
~SeqList() { delete [] elements; };
bool IsEmpty() const;
int Length() const;
bool Find(int k,T& x) const;
int Search(const T& x) const;
bool Insert(int k,const T& x);
bool Delete(int k);
bool Update(int k, const T&x);
void Output(ostream& out)const ;
private:
int length;
int MaxLength;
T *elements; //动态一维数组
};
template <class T>
SeqList<T>::SeqList(int MaxListSize)
{
MaxLength=MaxListSize;
elements=new T[MaxLength];
length=0;
}
template <class T>
bool SeqList<T>::IsEmpty() const
{
return length==0;
}
template <class T>
int SeqList<T>::Length() const
{
return length;
}
template <class T>
template <class T>
bool SeqList<T>::Find(int k,T &x) const
{
if (k<1 || k>length) {
cout<<"Out of Bounds"<<endl;
return false;
}
x=elements[k-1];
return true;
}
template<class T>
int SeqList<T>::Search(const T& x) const
{
for (int i=0;i<length;i++)
if (elements[i]==x) return ++i;
return 0;
}
template<class T>
bool SeqList<T>::Insert(int k,const T& x)
{
if (k<0 || k>length) {//判断k是否合法
cout<<"Out Of Bounds"<<endl;
return false;
}
if ( length== MaxLength ){ //判断是否满
cout<<"OverFlow"<<endl;
return false;
}
//将最后一个元素到第k+1个元素之间的数逐个后移
for (int i=length-1;i>=k;i--)
elements[i+1]=elements[i];
elements[k]=x;
length++;
return true;
}
template <class T>
bool SeqList<T>::Delete(int k)
{
if ( !length ) {//判断是否空
cout<<"UnderFlow"<<endl;
return false;
}
if ( k<1 || k>length ) {//判断K是否合法
cout<<"Out Of Bounds"<<endl;
return false;
}
//将第K+1个元素与最后一个元素之间的数逐个前移
for (int i=k;i<length;i++)
elements[i-1]=elements[i];
length--;
return true;
}
template <class T>
bool SeqList<T>::Update(int k, const T& x)
{
if (k<1|| k>length ) {
cout<<"Out Of Bounds"<<endl;
return false;
}
elements[k-1]=x;
return true;
}
template <class T>
void SeqList<T>::Output(ostream& out)const
{ for (int i=0; i< length; i++)
out<<elements[i]<<' '; //各元素间用空格隔开
out<<endl;
}
[解决办法]
先问一句哪个版本的VS?
c:\program files\microsoft visual studio\data structures\seqlist.h(45) : error C2059: syntax error : ''template<''
45行长什么样?
c:\program files\microsoft visual studio\data structures\seqlist.h(46) : error C2065: 'T' : undeclared identifier
46行长什么?
c:\program files\microsoft visual studio\data structures\seqlist.h(56) : error C2904: 'T' : template-name already defined as 'int T'
56行长什么样?
c:\program files\microsoft visual studio\data structures\seqlist.h(58) : error C2143: syntax error : missing ';' before '{'
58行长什么样?
c:\program files\microsoft visual studio\data structures\seqlist.h(58) : error C2447: missing function header (old-style formal list?)
同上
c:\program files\microsoft visual studio\data structures\seqlist.h(64) : error C2954: template definitions cannot nest
64行长什么样?