Results 1 to 6 of 6

Thread: QStack Help

  1. #1
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QStack Help

    hi all,

    i m implementing a stack. the contents of which are class stack.
    definition of class stack :
    Qt Code:
    1. //stack.h
    2. class cellSpec
    3. {
    4. int row,col;
    5. public:
    6. cellSpec(int,int);
    7. void operator=(const cellSpec & temp);
    8. };
    9.  
    10. class stack
    11. {
    12. QString lastAction;
    13. QTableWidget *table;
    14. QStringList Contents;
    15. QList<cellSpec> cellAdd;
    16. public:
    17. stack(QStringList Contents,QTableWidget *table,QList<cellSpec> cellAdd,QString lastAction="none");
    18. // stack(const stack &);
    19. // stack operator=(stack &);
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //stack.cpp
    2. #include "Stack.h"
    3.  
    4. cellSpec::cellSpec(int r,int c)
    5. {
    6. row = r;
    7. col = c;
    8. }
    9.  
    10. void cellSpec::operator=(const cellSpec & temp)
    11. {
    12. row = temp.row;
    13. col = temp.col;
    14.  
    15. }
    16.  
    17. stack::stack(QStringList Contents,QTableWidget *table,QList<cellSpec> cellAdd,QString lastAction)
    18. {
    19. lastAction = lastAction;
    20. table = table;
    21. Contents << Contents;
    22. cellAdd = cellAdd;
    23. }
    24. /*
    25. stack::stack(const stack &stack)
    26. {
    27. // return stack;
    28. }
    29. /*
    30. stack stack::operator=(stack &stack)
    31. {
    32. return stack;
    33. }
    34. */
    To copy to clipboard, switch view to plain text mode 

    i m receiving error :
    Qt Code:
    1. C:/Qt/4.1.2/include\QtCore/../../src/corelib/tools/qvector.h(323) : error C2512: 'stack' : no appropriate default constructor available
    2. C:/Qt/4.1.2/include\QtCore/../../src/corelib/tools/qlist.h(179) : while compiling class-template member function 'void __thiscall QVector<class stack>::realloc(int,int)'
    3. C:/Qt/4.1.2/include\QtCore/../../src/corelib/tools/qvector.h(362) : error C2512: 'stack' : no appropriate default constructor available
    4. C:/Qt/4.1.2/include\QtCore/../../src/corelib/tools/qlist.h(179) : while compiling class-template member function 'void __thiscall QVector<class stack>::realloc(int,int)'
    5. Stack.cpp
    To copy to clipboard, switch view to plain text mode 

    main prg
    Qt Code:
    1. void QGrid::pushUndo(QString lastAction,QTableWidget *table,QStringList Contents,int row,int col)
    2. {
    3. QStack<stack> undoStack;
    4. cellSpec cell(row,col);
    5. QList<cellSpec> cellList;
    6. cellList << cell;
    7. cellList.append(cell);
    8. stack row1(Contents,table,cellList,lastAction);
    9. undoStack.push(row1);
    10. }
    To copy to clipboard, switch view to plain text mode 

    What can be the problem
    Do what u r afraid to do, and the death of fear is sure.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStack Help

    Qt Code:
    1. QStack<stack> undoStack;
    To copy to clipboard, switch view to plain text mode 

    This requires a default (without parameters) constructor for the "stack" class, because QStack has to initialise itself with it. You should probably provide some sane default values for those four fields you use in the class.

  3. #3
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStack Help

    hi wysota,
    u r right, i added one more constructor in stack class with no parameters and no body, and it compiled fine. i don't understand one thing however:

    because QStack has to initialise itself with it.
    please explain ... if i hav to initialise QStack then y need constructor in the stack class for that ?

    pls do reply ...
    Do what u r afraid to do, and the death of fear is sure.

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QStack Help

    Quote Originally Posted by ankurjain
    hi wysota,
    u r right, i added one more constructor in stack class with no parameters and no body, and it compiled fine. i don't understand one thing however:



    please explain ... if i hav to initialise QStack then y need constructor in the stack class for that ?

    pls do reply ...
    Emm..you dont need stack constructor ..forgot it
    Declare QStack object in some your class declaration as class member...and then in constructor of this class use method push for default init your stack..is this that what you want?
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStack Help

    actually no,
    i want to do is, if some action is performed on table widget, like cut, paste etc, the previous state i want to store on stack, to do undo later.
    Do what u r afraid to do, and the death of fear is sure.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStack Help

    Quote Originally Posted by ankurjain
    please explain ... if i hav to initialise QStack then y need constructor in the stack class for that ?
    When you create a container like QStack, QVector or whatever, it reserves space for objects it will hold. When you resize the container, it internally creates "empty" objects of the type it holds, before you can fill them. That's why a default constructor is required -- to create an "empty" object. You can probably work it around, but you'll surely loose some of container functionality, so it's better to provide a default constructor.

  7. The following user says thank you to wysota for this useful post:

    ankurjain (5th May 2006)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.