In view.h file:
friend QDebug operator<< (QDebug , const Model_Personal_Info &);
view.cpp文件:
QDebug operator<< (QDebug out, const Model_Personal_Info &personalInfo) {
out << "Personal Info :n";
return out;
}
调用后:
qDebug() << personalInfo;
假设给出输出:"Personal Info :"
但是给出错误:
error: no match for 'operator<<' in 'qDebug()() << personalInfo'
Header:
class DebugClass : public QObject
{
Q_OBJECT
public:
explicit DebugClass(QObject *parent = 0);
int x;
};
QDebug operator<< (QDebug , const DebugClass &);
和实现:
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();
}
或者你可以这样定义all in header:
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;
};
尽管当前的答案可以解决问题,但是其中有很多多余的代码。把这个加到你的.h
里。
QDebug operator <<(QDebug debug, const ObjectClassName& object);
然后在你的.cpp
中这样执行。
QDebug operator <<(QDebug debug, const ObjectClassName& object)
{
// Any stuff you want done to the debug stream happens here.
return debug;
}