2013-11-01 38 views
0

节点已经是模板化函数。我已经将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; 
    } 

} 

Node.h

#ifndef NODE_H 
#define NODE_H 

template <class T> class Node { 
public: 
    T data; 
    Node<T>* next; 
public: 
    Node(T); 
    virtual ~Node(); //for later use of polymorphismi, review the topic again 

// friend class Stack; // allows dStack for private member access 
}; 

// constructor: create a new Node with d as data 
template <class T> 
Node<T>::Node(T data) { 
    this->data = data; 
    next = 0; 
} 

template <class T> 
Node<T>::~Node() { 
} 

#endif 

Stack.h

#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 
+0

在一百万年内,编译器不会说'最初堆栈已满并且出现错误。把实际的错误文本。 – MahanGM

+0

没有错误文本。这不是编译器的抱怨。代码检查堆栈是否为空然后报告它。 – user1082764

+0

你有没有在堆栈构造函数中有意注释掉topNode的初始化? – dnk

回答

1

topNode除非你取消对该行topNode = 0;不为空。您需要初始化它

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