Stack Template class



  • so i have the following classes:

    -node.h
    @
    #ifndef NODE_H
    #define NODE_H

    #include <QDebug>

    template<class T>
    class Node
    {
    public:
    Node(T invalue);
    ~Node() ;
    T getValue() const ;
    void setValue(T value) ;
    Node<T>* getNext() const ;
    void setNext(Node<T>* next) ;

    private:
        T m_Value;
        Node<T>* m_Next;
    

    };

    #endif // NODE_H
    @

    -node.cpp

    @
    #include "node.h"

    template<class T>
    Node<T>::Node(T invalue):
    m_Value(invalue), m_Next(0)
    {

    }

    template<class T>
    Node<T>::~Node()
    {
    qDebug() << m_Value << " deleted " << endl;
    if(m_Next)
    delete m_Next;
    }

    template<class T>
    T Node<T>::getValue() const
    {
    return m_Value;
    }

    template<class T>
    void Node<T>::setValue(T value)
    {
    m_Value = value;
    }

    template<class T>
    Node<T>* Node<T>::getNext() const
    {
    return m_Next;
    }

    template<class T>
    void Node<T>::setNext(Node<T>* next)
    {
    m_Next = next;
    }
    @

    -stack.h

    @
    #ifndef STACK_H
    #define STACK_H

    #include "node.h"

    template<class T>
    class Stack
    {
    public:
    Stack();
    ~Stack<T>();
    void push(const T& t);
    T pop();
    T top() const;
    int count() const;
    private:
    Node<T>* m_Head;
    int m_Count;
    };

    #endif // STACK_H

    -stack.cpp
    #include "stack.h"

    template <class T>
    Stack<T>::Stack():
    m_Head(0), m_Count(0)
    {

    }

    template <class T>
    Stack<T>::~Stack<T>()
    {
    delete m_Head;
    }

    template <class T>
    void Stack<T>::push(const T& value)
    {
    Node<T>* newNode = new Node<T>(value);
    newNode->setNext(m_Head);
    m_Head = newNode;
    ++m_Count;
    }

    template <class T>
    T Stack<T>::pop()
    {
    Node<T>* popped = m_Head;
    if (m_Head != 0)
    {
    m_Head = m_Head->getNext();
    T retval = popped->getValue();
    popped->setNext(0);
    delete popped;
    --m_Count;
    return retval;
    }
    return 0;
    }
    @

    main.cpp

    @
    #include <QtGui/QGuiApplication>
    #include <QDebug>
    #include "qtquick2applicationviewer.h"
    #include "stack.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/Stacks/main.qml"&#41;&#41;;
    viewer.showExpanded();
    
    Stack<int> s();
    s.push(3);
    
    
    return app.exec&#40;&#41;;
    

    }
    @

    with this resulting error:
    ../main.cpp:15: error: request for member 'push' in 's', which is of non-class type 'Stack<int>()'

    Please help



  • Hello and welcome to qt-project.org,

    • The formating of the Text is pretty bad. Could you place an @ in front and after your code, please? For example:

    bq. someheader.h
    @(at) Some code (at)@
    somesource.cpp
    @(at) Some code (at)@

    • It looks like a generell Cpp and not like a QML problem, so i would recommend you to post such things in the General Qt Forum which would be: "General and Desktop":http://qt-project.org/forums/viewforum/10/
      But now you're here and the QML guys will try to help you :)
    • I will take a closer look at it and tell you what i found out.


  • sorry for posting this here...i am working mostly with qtquick...and i wanna implement the Hanoi Tower problem with qml binding...so... again sorry for my ignorance :D



  • It's all fine, don't misunderstand me. Just for the next time because the guys in the general forum could probably help you out faster, but you can also post here, it's no problem at all. Thanks for formating your code, it looks cleaner and is easier to overview.



  • I'm not sure but could it be that your:
    @void Stack<T>::push(const T& value)@
    should look like that:
    @void Stack::push(const T& value)@



  • nope...
    @
    void Stack::push(const T&value)
    @
    gives the same error


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.