overload QTextStream::operator<<
-
I want to overload operator <<. How I can do it? I found similar topic, but my code produce error
#include <QTextStream> QTextStream &operator<<(QTextStream& out, const QString& string) { return out << string << ";"; } MainClass::MainClass(QObject *parent) : QObject(parent) { QString str; QTextStream s(&str); QString p("text"); s<< p; qDebug() << str; }
error: ambiguous overload for ‘operator<<’ (operand types are ‘QTextStream’ and ‘const QString’)
-
I want to overload operator <<. How I can do it? I found similar topic, but my code produce error
#include <QTextStream> QTextStream &operator<<(QTextStream& out, const QString& string) { return out << string << ";"; } MainClass::MainClass(QObject *parent) : QObject(parent) { QString str; QTextStream s(&str); QString p("text"); s<< p; qDebug() << str; }
error: ambiguous overload for ‘operator<<’ (operand types are ‘QTextStream’ and ‘const QString’)
@DungeonLords said in overload QTextStream::operator<<:
QTextStream &operator<<(QTextStream& out, const QString& string)
This function already exists, you can't overload it again.
-
@DungeonLords to add to @Christian-Ehrlicher :
...and because it's notvirtual
, you can't override this operator outside of theQTextStream
class.What you can do:
Subclass it and make your own stream class based onQTextStream
. There you can define your own<<
operator