Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Stack Template class
Forum Updated to NodeBB v4.3 + New Features

Stack Template class

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mititelud
    wrote on last edited by
    #1

    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

    :)

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on last edited by
      #2

      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.
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mititelud
        wrote on last edited by
        #3

        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

        :)

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onek24
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            onek24
            wrote on last edited by
            #5

            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)@

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mititelud
              wrote on last edited by
              #6

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

              :)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved