在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'发布于 2015-07-29 07:38:53
标题:
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;
};对我来说很好。
发布于 2016-04-18 17:22:56
尽管目前的答案很管用,但里面有很多代码是多余的。只需将此添加到您的.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;
}https://stackoverflow.com/questions/31693832
复制相似问题