1

在 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'
4

2 回答 2

2

标题:

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

对我来说很好。

于 2015-07-29T07:38:53.457 回答
1

尽管当前的答案可以解决问题,但其中有很多代码是多余的。只需将此添加到您的.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;
}
于 2016-04-18T17:22:56.493 回答