0

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,

samprat
  • 2,150
  • 8
  • 39
  • 73
  • 1
    There is no such operator, so you may have to pass `buffer.toStdString()` (or something like that) – juanchopanza Dec 02 '13 at 17:20
  • @juanchopanza, If i am not wrong you have helped me in the pas t as well. so thanks a lot. Just to make sure that I make understood your answer properly you mean that its wrong to use os << buffer as Qstring can be converted into ostream? – samprat Dec 02 '13 at 17:23
  • @samprat: ostream does NOT know Qt so it cannot accept any Qt classes as an argument of its functions. You need to give ostream something that he knows, like a std:string or a char*. – Guid Dec 02 '13 at 17:25
  • How about os << qPrintable(buffer); – drescherjm Dec 02 '13 at 17:25
  • 1
    You're welcome! It means that a `QString` cannot be "streamed" into an `std::ostream` because there is no `std::ostream& operator<<(std::ostream&, const QString&)`. So you have to "convert" it to something that does have such an operator, such as an `std::string`. Or a `const char*` via method `data()` for example. – juanchopanza Dec 02 '13 at 17:26
  • thanks everyone for the help.. I had no clue that we cant use QString with ostream,, as suggested by @juanchopanza, i have used os< – samprat Dec 02 '13 at 17:27

3 Answers3

2

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

dornhege
  • 1,500
  • 8
  • 8
1

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

Try this:

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

or

os << buffer.toUtf8().constData() 
Josh
  • 730
  • 7
  • 14
1

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?

samprat
  • 2,150
  • 8
  • 39
  • 73