Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Friend QDataStream from within NameSpace
Forum Updated to NodeBB v4.3 + New Features

Friend QDataStream from within NameSpace

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.2k 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.
  • S Offline
    S Offline
    stevenceuppens
    wrote on last edited by
    #1

    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;
    }
    @

    Steven CEUPPENS
    Developer &#x2F; Architect
    Mobile: +32 479 65 93 10

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stevenceuppens
      wrote on last edited by
      #2

      anyone? post is still open :)

      Steven CEUPPENS
      Developer &#x2F; Architect
      Mobile: +32 479 65 93 10

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You have two options:

        surround the operators implementation with your namespace

        prefix the operators with your namespace

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        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