PDA

View Full Version : QStack Help



ankurjain
5th May 2006, 10:52
hi all,

i m implementing a stack. the contents of which are class stack.
definition of class stack :


//stack.h
class cellSpec
{
int row,col;
public:
cellSpec(int,int);
void operator=(const cellSpec & temp);
};

class stack
{
QString lastAction;
QTableWidget *table;
QStringList Contents;
QList<cellSpec> cellAdd;
public:
stack(QStringList Contents,QTableWidget *table,QList<cellSpec> cellAdd,QString lastAction="none");
// stack(const stack &);
// stack operator=(stack &);

};



//stack.cpp
#include "Stack.h"

cellSpec::cellSpec(int r,int c)
{
row = r;
col = c;
}

void cellSpec::operator=(const cellSpec & temp)
{
row = temp.row;
col = temp.col;

}

stack::stack(QStringList Contents,QTableWidget *table,QList<cellSpec> cellAdd,QString lastAction)
{
lastAction = lastAction;
table = table;
Contents << Contents;
cellAdd = cellAdd;
}
/*
stack::stack(const stack &stack)
{
// return stack;
}
/*
stack stack::operator=(stack &stack)
{
return stack;
}
*/


i m receiving error :


C:/Qt/4.1.2/include\QtCore/../../src/corelib/tools/qvector.h(323) : error C2512: 'stack' : no appropriate default constructor available
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)'
C:/Qt/4.1.2/include\QtCore/../../src/corelib/tools/qvector.h(362) : error C2512: 'stack' : no appropriate default constructor available
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)'
Stack.cpp


main prg


void QGrid::pushUndo(QString lastAction,QTableWidget *table,QStringList Contents,int row,int col)
{
QStack<stack> undoStack;
cellSpec cell(row,col);
QList<cellSpec> cellList;
cellList << cell;
cellList.append(cell);
stack row1(Contents,table,cellList,lastAction);
undoStack.push(row1);
}


What can be the problem

wysota
5th May 2006, 10:58
QStack<stack> undoStack;

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.

ankurjain
5th May 2006, 11:31
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 ...

zlatko
5th May 2006, 11:40
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?

ankurjain
5th May 2006, 11:52
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.

wysota
5th May 2006, 12:44
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.