新手写类模板的时候遇到的问题

guofengzai 2008-04-17 12:21:50
//Stack.h
#ifndef STACK_H_
#define STACK_H_
const int MaxSize=50;
template <class T>
class Stack{
public:
Stack(){};
~Stack(){};
virtual void push(const T& x)=0;
virtual void pop()=0;
virtual T top()const=0;
virtual bool isEmpty()const=0;
virtual bool isFull()const=0;
};
#endif /*STACK_H_*/

//SeqStack.h
#ifndef SEQSTACK_H_
#define SEQSTACK_H_
#include"Stack.h"
template <class T>
class SeqStack:public Stack<T>{
public:
SeqStack(int MaxSize);
~SeqStack();
bool isEmpty()const;
void push(const T& x);
void pop();
T top()const{ return s[top];}
bool isFull()const;
void SetNull(){top=-1;}
private:
T *s;
int maxTop;
int top;//总是指向栈顶元素

};
#endif /*SEQSTACK_H_*/

//SeqStack.cpp
#include"SeqStack.h"
using namespace std;
template <class T>
SeqStack<T>::SeqStack(){
maxTop=MaxSize-1;
s=new T[MaxSize];
top=-1;
}

template <class T>
SeqStack<T>::~SeqStack(){
delete[] s;
}

template <class T>
void SeqStack<T>::push(const T& x){
s[++top]=x;
}

template <class T>
void SeqStack<T>::pop(){--top;}

//template <class T>
//T SeqStack<T>::top()const{return s[top];}

template <class T>
bool SeqStack<T>::isEmpty()const{
return top==-1;
}

template <class T>
bool SeqStack<T>::isFull()const{
return top==maxTop;
}






**** Build of configuration Debug for project C_unit3_stack ****

make -k all
make: Warning: File `objects.mk' has modification time 0.61 s in the future
Building file: ../SeqStack.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"SeqStack.d" -MT"SeqStack.d" -o"SeqStack.o" "../SeqStack.cpp"
In file included from ../SeqStack.cpp:1:
../SeqStack.h:18: error: declaration of `int SeqStack<T>::top'
../SeqStack.h:12: error: conflicts with previous declaration `T SeqStack<T>::top() const'
../SeqStack.cpp:4: error: prototype for `SeqStack<T>::SeqStack()' does not match any in class `SeqStack<T>'
../SeqStack.h:7: error: candidate is: SeqStack<T>::SeqStack(int)
../SeqStack.cpp:4: error: template definition of non-template `SeqStack<T>::SeqStack()'
make: *** [SeqStack.o] Error 1

make: Target `all' not remade because of errors.
make: warning: Clock skew detected. Your build may be incomplete.
Build complete for project C_unit3_stack
...全文
130 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
晨星 2008-04-17
  • 打赏
  • 举报
回复
至少在同一个作用域中,每个名字都应该有唯一的含义。
晨星 2008-04-17
  • 打赏
  • 举报
回复
top即作成员函数名,又作成员变量名,冲突了。
manan_ycb 2008-04-17
  • 打赏
  • 举报
回复
模板需要全部写到h文件里
guofengzai 2008-04-17
  • 打赏
  • 举报
回复
改了之后修正一些错误,但还有一些
//Stack.h
#ifndef STACK_H_
#define STACK_H_
const int MaxSize=50;
template <class T>
class Stack{
public:
Stack(){}
~Stack(){}
virtual void push(const T& x)=0;
virtual void pop()=0;
virtual T Top()const=0;
virtual bool isEmpty()const=0;
virtual bool isFull()const=0;
};
#endif /*STACK_H_*/



//SeqStack.h
#ifndef SEQSTACK_H_
#define SEQSTACK_H_
#include"Stack.h"
template <class T>
class SeqStack:public Stack<T>{
public:
SeqStack(int MaxSize);
~SeqStack();
bool isEmpty()const;
void push(const T& x);
void pop();
T Top()const{ return s[top];}
bool isFull()const;
void SetNull(){top=-1;}
private:
T *s;
int maxTop;
int top;//总是指向栈顶元素

};
#endif /*SEQSTACK_H_*/


//SeqStack.cpp
#include"SeqStack.h"
using namespace std;
template <class T>
SeqStack<T>::SeqStack(){
maxTop=MaxSize-1;
s=new T[MaxSize];
top=-1;
}

template <class T>
SeqStack<T>::~SeqStack(){
delete[] s;
}

template <class T>
void SeqStack<T>::push(const T& x){
s[++top]=x;
}

template <class T>
void SeqStack<T>::pop(){--top;}

//template <class T>
//T SeqStack<T>::top()const{return s[top];}

template <class T>
bool SeqStack<T>::isEmpty()const{
return top==-1;
}

template <class T>
bool SeqStack<T>::isFull()const{
return top==maxTop;
}


编译结果如下:

**** Build of configuration Debug for project C_unit3_stack ****

make -k all
make: Warning: File `subdir.mk' has modification time 1.4 s in the future
Building file: ../SeqStack.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"SeqStack.d" -MT"SeqStack.d" -o"SeqStack.o" "../SeqStack.cpp"
../SeqStack.cpp:4: error: prototype for `SeqStack<T>::SeqStack()' does not match any in class `SeqStack<T>'
../SeqStack.h:7: error: candidate is: SeqStack<T>::SeqStack(int)
../SeqStack.cpp:4: error: template definition of non-template `SeqStack<T>::SeqStack()'
make: *** [SeqStack.o] Error 1

make: Target `all' not remade because of errors.
make: warning: Clock skew detected. Your build may be incomplete.
Build complete for project C_unit3_stack



64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧