PDA

View Full Version : QTextStream question



seux
13th August 2011, 18:59
Hello everyone,
I had the following piece of code:


int iCounter = 0;
std::string str;
std::stringstream sstr;
sstr << "Number :" << "\t" << iCounter++ << "\t" << iCounter++ << "\t" << iCounter++;
str.append(sstr.str());

When i now output the string it looked like this:
Number : 2 1 0
instead of Number : 0 1 2
and I even got a warning that iCounter may be undefined...I do not know whats wrong there, I also tried it out with standard c++ in a console application and it worked.

So, I decided to switch to QTextStream instead of stringestream, which looks like this:

int iCounter = 0;
std::string str;
QTextStream sstr;
sstr << "Number :" << "\t" << iCounter++ << "\t" << iCounter++ << "\t" << iCounter++;
str.append(sstr.string()->toStdString());

But at the line of 5 the application crashes...Why is both not working?

stampede
13th August 2011, 22:50
But at the line of 5 the application crashes...


QString * QTextStream::string () const
Returns the current string assigned to the QTextStream, or 0 if no string has been assigned.

You forgot to assign a string to QTextStream, so it returns 0 (and crashes when you try to dereference NULL pointer), this should work:


int iCounter = 0;
std::string str;
QString string;
QTextStream sstr(&string);
sstr << "Number :" << "\t" << iCounter++ << "\t" << iCounter++ << "\t" << iCounter++;
str.append(string.toStdString());

But I think this could be done easier (I dont know the purpose, but anyway...):


int iCounter = 0;
std::string str;
QString string = "Number :";
for(int i=0 ; i<3 ; ++i) string += QString("\t%1").arg(iCounter++);
str.append(string.toStdString());

No need for QTextStream and operations on iCounter are well defined now.

seux
14th August 2011, 17:15
I got it working with your second solution, even with your first one i got the following output:
Number : 2 1 0
I also tried it out with

string = QString("Number: %1\t%2\t%3").arg(iCounter++).arg(iCounter++).arg(iCounter++);
with just the above output :(

I know that I got it, but I want to know why the other code is not working...Does somebody know what is going on there?

ChrisW67
14th August 2011, 23:24
My compiler issues a warning when compiling that line:


main.cpp:32: warning: operation on ‘iCounter’ may be undefined
main.cpp:32: warning: operation on ‘iCounter’ may be undefined
which is a flag that something is potentially broken.

You are assuming that the compiler executes the post-increment operators from left to right. That is clearly not what the compiler is doing: it assumes the three arguments to arg() are independent, therefore can be evaluated in any order, and evaluates them in 'reverse' order. I get a different (unexpected) result if I use the pre-increment operator. Generally, relying on side-effects and evaluation order within a single compound statement is fragile. The only guarantee the you get with that statement is that iCounter will have been incremented three times before the next statement is executed. Read about "sequence points" for more detail.

Try:


string = QString("Number: %1\t%2\t%3").arg(iCounter).arg(iCounter+1).arg(iCounter+2);
iCounter += 3;