PDA

View Full Version : QTextStream object



ShamusVW
20th November 2009, 06:44
I am trying to use a QTextStream object as a data member as follows:

[in .h file]

private:
QTextStream *out;
[in .cpp file]

QFile file(fileName);
if (!file.open(QIODevice::Append | QIODevice::Text))
return;

out = new QTextStream(&file);
out << "test";
...

but it has a compile time error
error: invalid operands of types `QTextStream*' and `const char[5]' to binary `operator<<'

I then modified it to


QTextStream o(&file);
out = &o;
out << "test";
but still the same error.
Can someone please tell me the correct way to do this?

Lykurg
20th November 2009, 06:51
You have to use
*out << "test";

ShamusVW
20th November 2009, 06:54
That was [very] quick.:)
Thanks.

But can you then tell me why? It seems different to how other objects get declared and used?

Lykurg
20th November 2009, 07:04
if you use
out << "foo"; out is treated as a normal "plain" pointer (without any knowledge of QTextStrem) and operator << is applied for that. Only ofter dereference you can use the operator.

ShamusVW
20th November 2009, 07:16
I understand, thanks for that.