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. How to implement << in a class for output to qDebug?
Forum Updated to NodeBB v4.3 + New Features

How to implement << in a class for output to qDebug?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 722 Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I want to implement an override for the operator << in my class so I can dump data to qDebug, what I've tried so far is obviously wrong.

    I have overloaded the operator << in another class, prototype:

    clsJSON& operator<<(const QVariant& crvarData);
    

    Implementation:

    clsJSON& clsJRONRef::operator<<(const QVariant& crvarData) {
        int intLength(intArrayLength());
        if ( intLength >= 0 ) {
            QString strRef(clsJRONRef::crstrMakeArrayRef(intLength));
            const QByteArray carybytRef[strRef::toLatin1());
            const char* cpszRef(carybytRef.data());
            QString strArrayLength(QString("%1%2%2")
                                      .arg(clsJSON::mscszALSM)
                                      .arg(++intLength)
                                      .arg(clsJSON::mscszALEM));
            mpobjJSON->setProperty(cpszRef, crvarData);
            mpobjJSON->setProperty(mcpszRef, QVariant(strArrayLength));
            mblnValid = true;
        }
        return *mpobjJSON;
    }   
    

    The above works and allows me to do stuff like:

    QByteArray arybytData;
    clsJSON objDemo, objJSONarray;
    arybytData.append("ABCDEFGH");
    objDemo["bytearray"] = arybytData;
    objJSONarray["array"] << 1;
    objJSONarray["array"] << 2;
    objJSONarray["array"] << 3;
    objJSONarray["array"] << "a";
    objJSONarray["array"] << "b";
    objJSONarray["array"] << "c";
    

    What I now want to do is the inverse, using the << operator to extract data from the object, something like:

    qDebug() << objDemo["bytearray"];
    qDebug() << objJSONarray;
    

    I've tried adding the prototype:

    friend ostream& operator<<(ostream& os, const clsJSON& objJSON);
    

    With implementation:

    ostream& clsJSON::operator<<(ostream& os, const clsJSON& crobjJSON) {
    ...
    }
    

    When I try to build this I get:

    std::ostream& clsJSON::operator<<(std::ostream&, const clsJSON&)' must take exactly one argument
    

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      If you add it as class function you must not add const clsJSON& crobjJSON - the value is in your object, also the function must be const in this case. If you want it as free function then the signature is correct except that you need qDebug instead std::ostream if you want to use it for qDebug - see https://doc.qt.io/qt-5/qdebug.html#writing-custom-types-to-a-stream

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by Kent-Dorfman
        #3
        This post is deleted!
        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