为什么会出现error C2146:
#include<stdio.h>
#include<stdlib.h>
template <typename ElemType>
class SqStack
{
public:
void clear();
int getLength();
int getstackSize();
Status getTop(ElemType& e);
bool isEmpty();
SqStack<ElemType>operator=(SqStack<ElemType>rightS);
Status pop(ElemType& e);
void push (ElemType& e);
SqStack();
virtual ~SqStack();
SqStack(const SqStack<ElemType>& otherS);
protected:
ElemType *base;
ElemType *top;
int stackSize;
};
template <typename ElemType>
void SqStack<ElemType>::clear()
{
base=top;
}
template <typename ElemType>
int SqStack<ElemType>::getLength()
{
return top-base;
}
template <typename ElemType>
int SqStack<ElemType>::getstackSize()
{
return SqStackSize;
}
template <typename ElemType>
Status SqStack<ElemType>::getTop(ElemType& e)
{
if(isEmpty())
return ERROR;
else
e=*(top-1);
return OK;
}
template <typename ElemType>
bool SqStack<ElemType>::isEmpty()
{
return (top==base?true:false);
}
template <typename ElemType>
SqStack<ElemType>SqStack<ElemType>::operator =(SqStack rightS)
{
int length=rights.getLength();
if(this!=&rightS)
{
if(stackSize<rightS.stackSize)
{
delect[] base;
base=new ElemType[right.stackSize];
assert(base!=0);
stackSize=rightS.stackSize;
}
for(int i=0;i<length;i++)
*(base+1)=*(rightS.base+i)
top=base+length();
}
return *this;
}
template <typename ElemType>
Status SqStack<ElemType>::pop(ElemType& e)
{
if(isEmpty())
return ERROR;
else
e=*--top;
return OK;
}
template <typename ElemType>
void SqStack<ElemType>::push(ElemType& e)
{
int length=top-base;
ElemType *newbase;
if(top-base>=stackSize)
{
newbase=new ElemType[stackSize+STACKINCREMENT];
assert(newbase!=0);
for(int j=0;j<length;j++)
*(newbase+j)=*(base+j);
delete []base;
stackSize+=STACKINCREMENT;
base=newbase;
top=base+length;
}
*top=e;
++top;
}
template <typename ElemType>
SqStack<ElemType>::SqStack()
{
base=new ElemType[STACK_MAX_SIZE];
assert(base!=0);
stackSize=STACK_MAX_SIZE;
top=base;
}
template <typename ElemType>
SqStack<ElemType>::~SqStack()
{
if(base)
delete []base;
stackSize=0;
top=base=NULL;
}
template <typename ElemType>
SqStack<ElemType>::SqStack(const SqStack& otherS)
{
int length;
base=new ElemType[otherS.stackSize];
assert(base!=0);
stackSize=otherS.stackSize;
for(int i=0;i<length;i++)
*(base+i)=*(otherS.base+i);
top=base+length;
}
总是出现这样的错误
ompiling...
q.cpp
f:\microsoft visual studio\myprojects\zhan\q.cpp(10) : error C2146: syntax error : missing ';' before identifier 'getTop'
f:\microsoft visual studio\myprojects\zhan\q.cpp(23) : see reference to class template instantiation 'SqStack<ElemType>' being compiled
f:\microsoft visual studio\myprojects\zhan\q.cpp(10) : error C2501: 'Status' : missing storage-class or type specifiers
f:\microsoft visual studio\myprojects\zhan\q.cpp(23) : see reference to class template instantiation 'SqStack<ElemType>' being compiled
f:\microsoft visual studio\myprojects\zhan\q.cpp(13) : error C2146: syntax error : missing ';' before identifier 'pop'
f:\microsoft visual studio\myprojects\zhan\q.cpp(23) : see reference to class template instantiation 'SqStack<ElemType>' being compiled
f:\microsoft visual studio\myprojects\zhan\q.cpp(13) : error C2501: 'Status' : missing storage-class or type specifiers
f:\microsoft visual studio\myprojects\zhan\q.cpp(23) : see reference to class template instantiation 'SqStack<ElemType>' being compiled
f:\microsoft visual studio\myprojects\zhan\q.cpp(40) : error C2143: syntax error : missing ';' before 'tag::id'
f:\microsoft visual studio\myprojects\zhan\q.cpp(40) : error C2501: 'Status' : missing storage-class or type specifiers
f:\microsoft visual studio\myprojects\zhan\q.cpp(40) : error C2059: syntax error : ';'
f:\microsoft visual studio\myprojects\zhan\q.cpp(40) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
zhan.exe - 8 error(s), 0 warning(s)
[解决办法]
你的Status没有定义
[解决办法]
- C/C++ code
#include<stdio.h>#include<stdlib.h>typedef int Status;template <typename ElemType>class SqStack{public: void clear(); int getLength(); int getstackSize(); Status getTop(ElemType& e); bool isEmpty(); SqStack<ElemType>operator=(SqStack<ElemType>rightS); Status pop(ElemType& e); void push (ElemType& e); SqStack(); virtual ~SqStack(); SqStack(const SqStack<ElemType>& otherS);protected: ElemType *base; ElemType *top; int stackSize;};template <typename ElemType>void SqStack<ElemType>::clear(){ base=top;}template <typename ElemType>int SqStack<ElemType>::getLength(){ return top-base;}template <typename ElemType>int SqStack<ElemType>::getstackSize(){ return SqStackSize;}template <typename ElemType>Status SqStack<ElemType>::getTop(ElemType& e){ if(isEmpty()) return ERROR; else e=*(top-1); return OK;}template <typename ElemType>bool SqStack<ElemType>::isEmpty(){ return (top==base?true:false);}template <typename ElemType>SqStack<ElemType>SqStack<ElemType>::operator =(SqStack rightS){ int length=rights.getLength(); if(this!=&rightS) { if(stackSize<rightS.stackSize) { delect[] base; base=new ElemType[right.stackSize]; assert(base!=0); stackSize=rightS.stackSize; } for(int i=0;i<length;i++) *(base+1)=*(rightS.base+i) top=base+length(); } return *this;}template <typename ElemType>Status SqStack<ElemType>::pop(ElemType& e){ if(isEmpty()) return ERROR; else e=*--top; return OK;}template <typename ElemType>void SqStack<ElemType>::push(ElemType& e){ int length=top-base; ElemType *newbase; if(top-base>=stackSize) { newbase=new ElemType[stackSize+STACKINCREMENT]; assert(newbase!=0); for(int j=0;j<length;j++) *(newbase+j)=*(base+j); delete []base; stackSize+=STACKINCREMENT; base=newbase; top=base+length; } *top=e; ++top;}template <typename ElemType>SqStack<ElemType>::SqStack(){ base=new ElemType[STACK_MAX_SIZE]; assert(base!=0); stackSize=STACK_MAX_SIZE; top=base;}template <typename ElemType>SqStack<ElemType>::~SqStack(){ if(base) delete []base; stackSize=0; top=base=NULL;}template <typename ElemType>SqStack<ElemType>::SqStack(const SqStack& otherS){ int length; base=new ElemType[otherS.stackSize]; assert(base!=0); stackSize=otherS.stackSize; for(int i=0;i<length;i++) *(base+i)=*(otherS.base+i); top=base+length;}int main(){ return 0;}
------解决方案--------------------