Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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
    {
    public:
    MyClass();
    ~MyClass();

    public:
    inline QString name() const { return m_name; }

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

    private:
    QString m_name;
    };

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

    }
    @

    .cpp file @
    using namespace MyNameSpace;

    MyClass::MyClass()
    {}

    MyClass::~MyClass()
    {}

    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

    Hi,

    You have two options:

    surround the operators implementation with your namespace

    prefix the operators with your namespace


Log in to reply