Overloading QDataStream & operator<< , operator>> in namespace
-
Hi! I need to overload operator<< and operator>> in order to write to and read from QDataStream these 2 types: QList<QList<QString> > and QMap<QString, QString>.
(For QMap<QString, QString> I can simply call
@operator<< <QString,QString>(out,map)@but for to have similar calls to both QList<QList<QString> and QMap<QString, QString>, I overload the last one too ).
SO, code:
@
#ifndef OPERATORS_H
#define OPERATORS_H#include <QDataStream>
namespace operatorsNamespace
{QDataStream & operator<< ( QDataStream & out, const QMap<QString, QString> & map ) { return operator<< <QString, QString> (out,map); } QDataStream & operator>> ( QDataStream & in, QMap<QString, QString> & map ) { return operator>> <QString,QString>(in,map); } QDataStream& operator>>(QDataStream& s, QList<QList<QString> >& l) { l.clear(); quint32 c; s >> c; l.reserve(c); for(quint32 i = 0; i < c; ++i) { QList<QString> t; operator>> <QString> (s, t); l.append(t); if (s.atEnd()) break; } return s; } QDataStream& operator<<(QDataStream& s, const QList<QList<QString> >& l) { s << quint32(l.size()); for (int i = 0; i < l.size(); ++i) operator<< <QString> (s, l.at(i)); return s; }
}
#endif // OPERATORS_H@
The issue is that, when I remove namespace wrapper, and call
@
QMap <QString,QString> fields;
QList <QList<QString> > content;
operator>>(str,fields);
operator>>(str,content);
@it's ok, program goes through these operators (stops at breakpoints). But when I add namespace wrapper, and call
@
operatorsNamespace::operator>>(str,fields);
operatorsNamespace::operator>>(str,content);
@, I get the following compiler errors :
@
In file included from client.cpp:18:
operators.h: In function 'QDataStream& operatorsNamespace::operator<<(QDataStream&, const QMap<QString, QString>&)':
operators.h:11: error: expected primary-expression before ',' token
operators.h:11: error: expected primary-expression before '>' token
operators.h:11: warning: left-hand operand of comma has no effect
operators.h: In function 'QDataStream& operatorsNamespace::operator>>(QDataStream&, QMap<QString, QString>&)':
operators.h:16: error: expected primary-expression before ',' token
operators.h:16: error: expected primary-expression before '>' token
operators.h:16: warning: left-hand operand of comma has no effect
operators.h: In function 'QDataStream& operatorsNamespace::operator>>(QDataStream&, QList<QList<QString> >&)':
operators.h:28: error: expected primary-expression before '>' token
operators.h:28: warning: left-hand operand of comma has no effect
operators.h: In function 'QDataStream& operatorsNamespace::operator<<(QDataStream&, const QList<QList<QString> >&)':
operators.h:40: error: expected primary-expression before '>' token
operators.h:40: warning: left-hand operand of comma has no effect
@Please, help me with this. Thanks in advance, gurus..
-
Weird that you need to implement the operator at all. After all, in the QList documentation you can find that there already is a an implemenation for operator<<() and operator>>(), that are supported for all types that themselves support these operators. As QString supports them, so should QList<QString>, and thus so should QList<QList<QString> > .
-
I'm not a namespace expert, but from my understanding:
QDataStream is in global Namespace (or Qt namespace). So I would try ::QDataStream, as QDataStream would refer to a member within your current namespace.
Albeit, Andre is correct: Why do you need to implement the operators in the first place?