1

In view.h file :

friend QDebug operator<< (QDebug , const Model_Personal_Info &);

In view.cpp file :

QDebug operator<< (QDebug out, const Model_Personal_Info &personalInfo) {
    out << "Personal Info :\n";
    return out;
}

after calling :

qDebug() << personalInfo;

It is suppose to give output : "Personal Info :"

but it is giving an error :

error: no match for 'operator<<' in 'qDebug()() << personalInfo'
Matt
  • 74,352
  • 26
  • 153
  • 180
Jyo the Whiff
  • 829
  • 9
  • 23
  • Jyo, please do not add "solved" or a solution to your question... post any solutions as answers. – Matt Aug 05 '15 at 17:59

2 Answers2

2

Header:

class DebugClass : public QObject
{
    Q_OBJECT
public:
    explicit DebugClass(QObject *parent = 0);
    int x;
};

QDebug operator<< (QDebug , const DebugClass &);

And realization:

DebugClass::DebugClass(QObject *parent) : QObject(parent)
{
    x = 5;
}   

QDebug operator<<(QDebug dbg, const DebugClass &info)
{
    dbg.nospace() << "This is x: " << info.x;
    return dbg.maybeSpace();
}

Or you could define all in header like this:

class DebugClass : public QObject
{
    Q_OBJECT
public:
    explicit DebugClass(QObject *parent = 0);
    friend QDebug operator<< (QDebug dbg, const DebugClass &info){
        dbg.nospace() << "This is x: " <<info.x;
        return dbg.maybeSpace();
    }

private:
    int x;
};

Works fine for me.

t3ft3l--i
  • 1,372
  • 1
  • 14
  • 21
2

Even though the current answer does the trick, there's a lot of code in there that's redundant. Just add this to your .h.

QDebug operator <<(QDebug debug, const ObjectClassName& object);

And then implement like so in your .cpp.

QDebug operator <<(QDebug debug, const ObjectClassName& object)
{
    // Any stuff you want done to the debug stream happens here.
    return debug;
}
Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99