Reputation: 839
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'
Upvotes: 1
Views: 2260
Reputation: 17303
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;
}
Upvotes: 2
Reputation: 1412
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.
Upvotes: 2