Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Overloading QDataStream & operator<< , operator>> in namespace
Forum Updated to NodeBB v4.3 + New Features

Overloading QDataStream & operator<< , operator>> in namespace

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 3 Posters 6.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bright day
    wrote on last edited by
    #1

    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..

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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> > .

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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?

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved