Stack without libraries

I have programmed a stack without libraries, however I am finding it difficult to find the error i have made and also if there is anything else that i need to include for my stack to work?
#include <iostream>
using namespace std;

template <typename T>
class Stack
{
public:

~Stack(void);
void push(T value);
Node<T>* nodePop(void);
T pop(void);
protected:
int count = 0;
Node<T> *pTop = nullptr;
};

class Node
{
public:
T value;
Node *pNext = nullptr;
};

template <typename T>
class Stack
{
public:
~stack(void);
void Push(T value);
Node<T>* NodePop(void);
T Pop(void);

private:
Node<T> *pTop = nullptr;
};

Stack<T>::~Stack(void)
{
while (pTop != nullptr) { delete NodePop(); }
}

template <typename T>

void Stack<T>::Push(T value)
{
Node<T> *pTmp = pTop;
if (pTop != nullptr) pTop = pTop->pNext;
return pTmp;
}

template <typename T>

T Stack<T>::Pop(void)
{
Node<T> *pTmp = NodePop();
int ret = 0;

if (pTmp != nullptr)
{
ret = pTmp->value;
}
else
{
throw "Stack Empty";
}
delete pTmp;

return ret;
}

void Stack<T>::push(T value)
{
Node<T>* pTmp = new Node<T>;
pTmp->value = value;
pTmp->pNext = pTop;
pTop = pTmp;
}

int main(void)
{
Stack<int> MyStack;

for (int count = 0; count < 20; count++)
{
MyStack.push(count);
}
try
{
cout << "Stack output:" << endl;

while (true)
{
cout << MyStack.pop() << " ";
}
}
catch (...)
{
cout << endl;
}

return 0;
}
What you posted won't compile.

Line 10: Node is undefined.

Line 17: Node needs to be a template because it uses T. It also needs to precede stack.

Lines 25-36: Stack is declared twice.

Line 31: NodePop () is not implemented.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cassert>

template < typename T > class stack
{
    // note: move semantics ignored
    public:

        stack() = default ;
        ~stack() { while( !empty() ) pop() ; }

        // non-copyable
        stack( const stack<T>& ) = delete ;
        stack<T>& operator= ( const stack<T>& ) = delete ;

        bool empty() const { return p_top == nullptr ; }

        void push( const T& value ) { node* n = new node( value, p_top ) ; p_top = n ; }

        void pop() // invariant: !empty()
        { assert( !empty() ) ; node* n = p_top ; p_top = p_top->next ; delete n ; }

        const T& top() const { assert( !empty() ) ;  return p_top->value ; } // invariant: !empty()
        T& top() { assert( !empty() ) ; return p_top->value ; } // invariant: !empty()

    private:

        struct node
        {
            node( T v, node* n = nullptr ) : value(v), next(n) {}
            T value ;
            node* next ;
        };

        node *p_top = nullptr ;
};

int main()
{
    stack<int> stk ;
    for( int i = 0 ; i < 20 ; ++i ) stk.push(i) ;

    while( !stk.empty() )
    {
        std::cout << stk.top() << ' ' ;
        stk.pop() ;
    }
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/9a9ba0da85f31a1f
Topic archived. No new replies allowed.