PDA

View Full Version : problem with QList



yamen ajjour
21st August 2011, 14:57
Hi guys i've created two classes one called
circle


class circle
{
public:
circle();
QString name ;
int id ;

};


and another use this



class soso
{
public:
soso();
QList<circle*> lis;
void go();
};



in the constructor of soso i've just added two circle



soso::soso()
{
circle* c1 = new circle();
circle* c2= new circle();
c1->id=1;
c1->name="gimy";
c2->id=2;
c2->name="susana";
lis.append(c1);
lis.append(c2);
}


and in the main window i've called go method which is included here


void soso::go()
{

QFile file("database.txt");
if(!file.open(QIODevice::WriteOnly))
throw " cannot open file ! ";
QDataStream out(&file);
int i=0;
QList<circle*>::iterator it1 =lis.begin();

for(i=0;it1!=lis.end();it1++);
{
out<<(*it1)->id; // segmentation error in the runtime
out<<(*it1)->name;
}
}


but i am getting segmentation error any body know why ?

softghost
21st August 2011, 15:44
your loop must be like these line


for(i=0;it1!=lis.end();it1++);
{
out<<(*it1).id;
out<<(*it1).name;
}

yamen ajjour
21st August 2011, 16:19
your loop must be like these line


for(i=0;it1!=lis.end();it1++);
{
out<<(*it1).id;
out<<(*it1).name;
}

i don't think so mate :)

norobro
21st August 2011, 19:21
I couldn't get the for loop to work either, but the following seems to work fine:
QList<circle* >::iterator it1=lis.begin();
while(it1!=lis.end())
{
out << (*it1)->id << (*it1)->name;
++it1;
}

EDIT: The following worked after I ran make clean and qmake:
QList<circle* >::iterator it1;
for(it1=lis.begin();it1!=lis.end();++it1) {
out<< (*it1)->id << (*it1)->name;
}

or you could do away with the iterator and use:
foreach(circle *c, lis) out << c->id << c->name;

yamen ajjour
22nd August 2011, 08:17
I couldn't get the for loop to work either, but the following seems to work fine:
QList<circle* >::iterator it1=lis.begin();
while(it1!=lis.end())
{
out << (*it1)->id << (*it1)->name;
++it1;
}

EDIT: The following worked after I ran make clean and qmake:
QList<circle* >::iterator it1;
for(it1=lis.begin();it1!=lis.end();++it1) {
out<< (*it1)->id << (*it1)->name;
}

or you could do away with the iterator and use:
foreach(circle *c, lis) out << c->id << c->name;

thank you very much i've found the problem the semicolon after the for loob caused the problem so we are accessing the end end element



void soso::go()
{

QFile file("database.txt");
if(!file.open(QIODevice::WriteOnly))
throw " cannot open file ! ";
QDataStream out(&file);
int i=0;
QList<circle*>::iterator it1 =lis.begin();

for(i=0;it1!=lis.end();it1++);
{
out<<(*it1)->id; // segmentation error in the runtime
out<<(*it1)->name;
}
}