Friend QDataStream from within NameSpace

  • Hi All,

    I want to use the QDataStream operators on a custom class.

    My class is within it's own namespace, which makes the friend declaration of this QDataStream operators faling...

    if i try to acces the private member "m_name" from within the QDataStream operators, it's not working, compiler is complaining that m_name is a private member.

    if i remove the namespace declaration "MyNameSpace", all is working fine..

    .h file
    #include <QtCore/QString>

    namespace MyNameSpace {

    class MyClass

    inline QString name() const { return m_name; }

    friend QDataStream &operator <<(QDataStream &out, const MyClass &class);
    friend QDataStream &operator >>(QDataStream &in, MyClass &class);

    QString m_name;

    QDataStream &operator <<(QDataStream &out, const MyClass &class);
    QDataStream &operator >>(QDataStream &in, MyClass &class);


    .cpp file @
    using namespace MyNameSpace;



    QDataStream &operator <<(QDataStream &out, const MyClass &class)
    return out << class.m_name;

    QDataStream &operator >>(QDataStream &in, MyClass &class)
    return in >> class.m_name;

  • anyone? post is still open :)

  • Lifetime Qt Champion


    You have two options:

    surround the operators implementation with your namespace

    prefix the operators with your namespace

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.