Error: no operand "<<" matches these operands when try to use Qstring with OStream object

279 views Asked by At

Qt version 5.01
Platform windows 64 bit

Issues:error: no operand "<<" matches these operands

I have looked into the web but could not able to find solution for it. I have tried lots of things but it doesnt work. If I change QString to std::string, then it compile properly . But then I will lossing sprintf functionality of QString which I need it.

ii) Secondly I have tried to delcare same function name in header file with extern keyword as suggested by someone in other forum but it doesnt work. SO May you guys please let know where I am making mistake. Below is my code

#include <sstream>
#include <QString>
#include <string>
#include <QTime>

namespace
    {
      std::ostream& operator <<(std::ostream& os ,  const QTime& system_time )
      {
        QString buffer ;
        const double seconds = double( system_time.second() ) + ( system_time.msec()/1000.0 ) ;
        buffer.sprintf( "%02i:%02i:%06.3f", system_time.hour(), system_time.minute(), seconds );
        os << buffer; // error here
        return os;
      }
    }

Thanks and regards,

3

There are 3 answers

0
Josh On

QString cannot be output that simply unless you overload the << operator.

Try this:

os << buffer.toAscii().constData()

or

os << buffer.toUtf8().constData() 
0
dornhege On

Qt provides qPrintable(someQString) to make it compatible with cout or printf.

0
samprat On

Thanks everyone for the help.. I had no clue that we cant use QString with ostream,, as suggested by @juanchopanza, i have used
os<<buffer.tostdString()

and it compiles fine now.. Thanks Everyone ... No idea how to mark this Question as solved !!!

Also wondering can we use QCString in qt 5.1?