C++ 如何将类更改为模板化类

C++ 如何将类更改为模板化类,c++,C++,节点已经是模板化函数。我已经尽我所能将Main.cpp和Stack.h更改为模板类,但由于某些原因,它不起作用。它进行编译,但最初表示堆栈已满,出现了问题 Main.cpp #include <iostream> using namespace std; #include "Stack.h" void stack_test(); int main() { stack_test(); return 0; } void stack_test() { /

节点已经是模板化函数。我已经尽我所能将Main.cpp和Stack.h更改为模板类,但由于某些原因,它不起作用。它进行编译,但最初表示堆栈已满,出现了问题

Main.cpp

#include <iostream>
using namespace std;

#include "Stack.h"

void stack_test();

int main() {

    stack_test();

    return 0;
}

void stack_test() {
    // Instantiate S1 as an int and S2 as a string
    Stack<int> s1;
    Stack<string> s2;

    // Stack empty test for both S1 and S2
    if (s1.isEmpty()) {
        cout << "s1 is empty at the beginning." << endl;
    } else {
        cout << "s1 must be empty. Something's wrong!" << endl;
    }
        if (s2.isEmpty()) {
        cout << "s2 is empty at the beginning." << endl;
    } else {
        cout << "s2 must be empty. Something's wrong!" << endl;
    }

    // Fill S1 and S2 with five int and five strings respectively
    for(int i = 0; i < 5; i++)
    s1.push(1 * (i + 1));
    string one = "1", two = "2", three = "3", four = "4", five = "5";
    s2.push(one);
    s2.push(two);
    s2.push(three);
    s2.push(four);
    s2.push(five);


    // pop() test: reverses the items
    cout << "Expected Ints: 5 4 3 2 1 -->" << endl;
    for (int i = 0; i < 5; i++) {
        cout << s1.top() << endl;
        s1.pop();
    }


    // Stack empty test
    if (s1.isEmpty()) {
        cout << "s1 is empty after five pop() calls." << endl;
    } else {
        cout << "s1 must be full. Something's wrong!" << endl;
    }
    if (s2.isEmpty()) {
        cout << "s2 is empty after five pop() calls." << endl;
    } else {
        cout << "s2 must be full. Something's wrong!" << endl;
    }

    // StackEmptyException test
    try {
        cout << "One more pop when the stack is empty..." << endl;
        s1.pop();
    } 
    catch (Stack<int>::StackEmptyException) {
        cout << "Exception: cannot pop, stack is empty" << endl;
    }
    try {
        cout << "One more pop when the stack is empty..." << endl;
        s2.pop();
    } 
    catch (Stack<string>::StackEmptyException) {
        cout << "Exception: cannot pop, stack is empty" << endl;
    }

}
#包括
使用名称空间std;
#包括“Stack.h”
无效堆栈_测试();
int main(){
堆栈_测试();
返回0;
}
无效堆栈_测试(){
//将S1实例化为int,将S2实例化为字符串
堆栈s1;
堆栈s2;
//S1和S2的堆栈空测试
if(s1.isEmpty()){

除非取消注释行
topNode=0;
,否则cout
topNode
不为空。您需要初始化它

template< class T>
Stack<T>::Stack() : topNode(NULL) 
{                   ^^^^^^^^^^^^          
}
模板
堆栈::堆栈():topNode(NULL)
{                   ^^^^^^^^^^^^          
}

编译器一开始不会说
堆栈已满,有些地方出错。
一百万年后。输入实际的错误文本。没有错误文本。这不是编译器抱怨的。代码检查堆栈是否为空,然后报告。您是否有意在堆栈常量中注释掉topNode的初始化Structor?正如@dnk所说,为什么
topNode
会被评论?
isEmpty()
不会那样工作的……谢谢dnk。IDK我怎么会错过这个。
#ifndef STACK_H
#define STACK_H

#include <cstdlib>
#include <iostream>
#include <new>
using namespace std;

#include "Node.h"

template <class T> class Stack {
private:
    Node<T>* topNode;
public:

    class StackEmptyException { }; 

    Stack();
    void push(T);
    void pop();
    bool isEmpty();
    bool isFull();
    double top();
    virtual ~Stack();

};

// constructor: new stack is created. topNode is null.
template< class T>
Stack<T>::Stack() {
 //   topNode = 0;
}


// push a data onto the stack
template< class T>
void Stack<T>::push(T data) {
    try {
        Node<T>* newNode = new Node<T>(data);
        newNode->next = topNode;
        topNode = newNode;
    } catch (bad_alloc &e) {
        cout << "memory allocation exception: " << e.what() << endl;
        exit(1);
    }
}

// pop the data from the stack
template< class T>
void Stack<T>::pop() {
    if (isEmpty())
        throw StackEmptyException();

    Node<T>* discard = topNode;
    topNode = topNode->next;
    delete discard;
}

// is stack empty?
template< class T>
bool Stack<T>::isEmpty() {
    return (topNode == 0);
}

// is stack full? 
template< class T>
bool Stack<T>::isFull() {
    return false; // never, unless memory is full
}

// read the data on the top of the stack
template< class T>
double Stack<T>::top() {
    if (isEmpty())
        throw StackEmptyException();

    return topNode->data;

}




// destructor: free all the memory allocated for the stack
template<class T>
Stack<T>::~Stack() {
    while (!isEmpty())
        pop();
}


#endif
template< class T>
Stack<T>::Stack() : topNode(NULL) 
{                   ^^^^^^^^^^^^          
}