Somthing wrong with the pointers???
Jan 14, 2015 at 1:57pm UTC
Hey, the first part is working code
1 2 3 4 5 6 7 8 9 10 11 12 13
QFile file("c:/test/test.txt" );
QFile *pFile=&file;
if (!pFile->open(QIODevice::WriteOnly)){
qDebug("file didn't open " );
return ;
}
//write to file
QDataStream out(pFile);
out.setVersion(QDataStream::Qt_5_4);
QDataStream *pStream=&out;
*pStream << MyNum << map << comp;
pFile->flush();
pFile->close();
now I want to put everything in a class,
but it doesn't work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
void main
{
Serializer sO("c:/test/test.txt" );
QDataStream *pStream = sO.getObject();
* pStream<< MyNum << map << comp;
sO.writeToFile();
}
class Serializer
{
public :
Serializer();
Serializer(QString filePath);
~Serializer();
// QDataStream *pStream;
QDataStream * getObject()
{
QFile file(this ->filePath);
this ->pFile = & file;
// QFile *pFile = & file;
if (!file.open(QIODevice::WriteOnly)){
qDebug("file didn't open " );
return nullptr ;
}
//write to file
QDataStream out(this ->pFile);
out.setVersion(QDataStream::Qt_5_4);
// out << MyNum << map << comp
QDataStream *pOut = &out;
return pOut;
}
void writeToFile()
{
(*(this ->pFile)).flush();
(*(this ->pFile)).close();
}
private :
QString filePath;
QFile *pFile; // enkel intern nodig
};
Jan 14, 2015 at 2:12pm UTC
Both file
and out
are local variables. They are destroyed as soon as the function ends. Thus you cannot use pointers to them outside of their function. Use new
in this case.
Jan 14, 2015 at 3:10pm UTC
ok, yes of course... the pointer survives but not the object...
thanx very much
Topic archived. No new replies allowed.