在view.h文件中:
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'
答案 0 :(得分:1)
部首:
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();
}
或者您可以像这样在标题中定义所有内容:
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;
};
对我来说很好。
答案 1 :(得分:0)
尽管目前的答案很明显,但那里的许多代码都是多余的。只需将其添加到.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;
}