1
votes

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'
2
Jyo, please do not add "solved" or a solution to your question... post any solutions as answers.Matt

2 Answers

2
votes

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.

1
votes

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