For your original code
Printer() << stdstring << qstring
equals to
(Printer().operator<<(stdstring)).operator<<(qstring)
You can see the problem here,
Printer() << stdstring
will return a ostream &
, and you pass QString
to ostream
. I think you should return Printer
other than the stream.
class Printer {
public:
virtual ~Printer(void) {
std::cout << o.str();
}
std::stringstream o;
};
Printer &operator<<(Printer &p, const std::string &s)
{
p.o << s;
return p;
}
Printer &operator<<(Printer &p, const QString &s)
{
p.o << s.toStdString();
return p;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString qstring("qstring");
std::string stdstring("std::string");
Printer p;
p << stdstring << qstring;
return a.exec();
}