注册 登录
编程论坛 C++教室

大家帮忙看看我的通用栈错在哪里

PENGGB023 发布于 2015-10-30 12:13, 405 次点击
//stack.h
#ifndef STACK_H_
#define STACK_H_

template <class T>
class LinkStack
{
public:
    LinkStack(){top = NULL;}
    ~LinkStack() {};
    struct node {
        T data;
        node *next;
    };
    void SetEmpty();
    bool IsEmpty();
    bool Push(T element);
    bool Pop(T & element);
     void show();
private:
    node * top;
};

#endif

// stack.cpp
#include "stack.h"

template <class T> void LinkStack <T>::SetEmpty()
{
    node * temp;
    while (top != NULL)
    {
        temp = top;
        top = top->next;
        delete temp;
    }
}

template <class T> bool LinkStack <T>::IsEmpty()
{
    return top == NULL;
}

template <class T> bool LinkStack <T>::Push(T element)
{
    node *temp = new node();
    if (temp == NULL)
        return false;
    temp->data = element;
    temp->next = top;
    top = temp;
    return true;
}

template <class T> bool LinkStack <T>::Pop(T & element)
{
    if (IsEmpty())
        return false;
    node *q = top;
    element = top->data;
    top = top->next;
    delete q;
    return true;
}

template <class T> void LinkStack <T>::show(T & element)
{
    node *temp = top;
    std::cout << std::endl << "----top----" << std::endl;
    while (temp->next != NULL)
    {
        std::cout << "    " << temp->data <<std::endl;
        temp = temp->next;
    }
    std::cout << "----top----" << std::endl << std:endl;
}

//main.cpp
#include <iostream>
#include "stack.h"

int main(int argc, char **argv)
{
    LinkStack<int> st;
    int a = 7;
    st.Push(a);
    st.Push(5);
    st.Push(8);
    st.show();
    return 0;
}
4 回复
#2
rjsp2015-10-30 12:34
错在哪儿编译器会告诉你的
#3
PENGGB0232015-10-30 14:33
他说我调用的那些函数都没有定义
#4
rjsp2015-10-30 14:47
以下是引用PENGGB023在2015-10-30 14:33:26的发言:

他说我调用的那些函数都没有定义
你说的我又听不懂,因为我不知道你说的“那些函数”是哪些函数。
我想你的编译器应该会说出是哪些函数没有定义的吧,绝不会说“那些函数没有定义”
#5
yangfrancis2015-11-01 21:17
show函数定义的时候没有参数,先把这个解决了再说其他问题。
1