静态堆栈模板

时间:2015-03-25 15:48:49

标签: c++

我正在尝试创建静态堆栈模板,但它不起作用,我不知道如何修复此错误,错误是: - "未定义参考' Stack :: Stack& #39; &#34 ;.请有人帮助我。

Stach.h文件: -

#ifndef STACK_INCLUDED
#define STACK_H_INCLUDED
#include <iostream>

using namespace std;

template <typename T>
class Stack{
private:
T *stackArray;
int stackSize;
int top;
public:
Stack(int);
Stack(const Stack &obj);
void push(T);
void pop(T&);
bool isFull();
bool isEmpty();
};
#endif // STACK_H_INCLUDED

Stack.cpp文件: -

#include <iostream>
#include"Stack.h"

using namespace std;

template <typename T>
Stack <T> :: Stack (int sz){
stackArray = new T[sz];
stackSize = sz;
top = -1;
}

template <typename T>
Stack <T> :: Stack (const Stack &obj){
if(obj.stackSize > 0){
    stackArray = new T[obj.stackSize];
}
else
    stackArray = NULL;
for (int i=0; i<stackSize; i++){
    stackArray[i] = obj.stackArray[i];
}
top = obj.top;
}

template <typename T>
void Stack <T> :: push(T num){
if(isFull()){
    cout<<"Stack Is Full!!"<<endl;
}
else{
    top++;
    stackArray[top] = num;
}
}

template <typename T>
void Stack <T> :: pop(T &num){
if(isEmpty()){
    cout<<"Stack Is Empty!!"<<endl;
}
else{
    num = stackArray[top];
    top--;
}
}

template <typename T>
bool Stack <T> :: isFull(){
bool status;
if(top == stackSize-1)
   status = true;
else
    status = false;
return status;
}

template <typename T>
bool Stack <T> :: isEmpty(){
bool status;
if(top == -1)
   status = true;
else
    status = false;
return status;
}

主档案: -

#include <iostream>
#include"Stack.h"

using namespace std;

int main()
{
int catchVar;
Stack <double> st(3);
st.push(2);
st.push(4);
st.push(6);
st.push(8);
cout<<"Elements Of The Stack Are:- ";
st.pop(catchVar);
cout<<catchVar<<endl;
st.pop(catchVar);
cout<<catchVar<<endl;
st.pop(catchVar);
cout<<catchVar<<endl;
st.pop(catchVar);
return 0;
}

0 个答案:

没有答案