Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm having problem making this linked stack working. The compiler is giving me errors and I've been checking for half an hour and still could not figure out what is wrong with my code. Would anyone take a look at and tell me what's wrong with it ?

LinkedNode.h

#include<memory>

template<class T>
class LinkedNode
{
    friend class LinkedStack<T>;

    public:
        LinkedNode(T newElement);
        T GetElement() const {return element;}
        void SetElement(T val) {element = val;}
        LinkedNode<T>* GetNext() const {return next;}
        void SetNext(LinkedNode<T>* val) {next = val;}
    private:
        T element;
        LinkedNode<T>* next;
};

template<class T>
LinkedNode<T>::LinkedNode(T newElement)
{
    element = newElement;
}

LinkedStack.h

#pragma once
#include"LinkedNode.h"
#include<cassert>

template<class T>
class LinkedStack
{
    public:
        LinkedStack();
        int GetSize() const {return size;}
        bool IsEmpty() const {return size == 0;}
        void Push(T val);
        void Pop();
        T Peek();
        void Clear();
    private:
        LinkedNode<T>* head;
        int size;

};

template<class T>
LinkedStack<T>::LinkedStack():size(0) {}

template<class T>
void LinkedStack<T>::Push(T val)
{
    LinkedNode<T>* newOne = new LinkedNode<T>(T val);
    if(head == 0)
        head = newOne;
    else
    {
        newOne->next = head;
        head = newOne;
    }
    size++;
}

template<class T>
void LinkedStack<T>::Pop()
{
    assert(!IsEmpty());
    LinkedNode<T>* tpHead = head;
    head = head->next;
    delete tpHead;
    size--;
}

template<class T>
T LinkedStack<T>::Peek()
{
    assert(!IsEmpty());
    return head->element;
}

template<class T>
void LinkedStack<T>::Clear()
{
    while(!IsEmpty())
        Pop();
}

Source.cpp

#include"LinkedStack.h"
#include<iostream>
#include<string>
#include<memory>

int main()
{
    LinkedStack<int> stack;

    std::cout << "Add" << std::endl;
    stack.Push(1);
    stack.Push(2);
    stack.Push(3);
    stack.Push(4);
    stack.Push(5);
    std::cout << "Top element is: " << stack.Peek();
    stack.Pop();
    std::cout << "After pop one out, its top element is: " << stack.Peek() << std::endl;

    return 0;
}
share|improve this question

closed as off-topic by Vedran Šego, palacsint, Nikita Brizhak, Elias Van Ootegem, Brian Reichle Oct 9 '13 at 13:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After your code is working you can edit this question for reviewing your working code." – Vedran Šego, palacsint, Nikita Brizhak, Elias Van Ootegem, Brian Reichle
If this question can be reworded to fit the rules in the help center, please edit the question.

    
In this site only working codes are reviewed. You need StackOverflow. –  Vedran Šego Oct 9 '13 at 10:41
add comment

1 Answer

One error will be in this line:

LinkedNode<T>* newOne = new LinkedNode<T>(T val);

It should actually be:

LinkedNode<T>* newOne = new LinkedNode<T>(val);

Another thing that I noticed, but won't probably give you a compilation error, is that you don't initialize the head pointer in the LinkedStack constructor to 0.

(On a related note, you should probably use nullptr instead of 0 for this).

share|improve this answer
    
thank you for your help. –  Hoang Minh Oct 9 '13 at 19:05
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.