如何重载操作符<<为qDebug [英] How to overload operator<< for qDebug

查看:284
本文介绍了如何重载操作符<<为qDebug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为我的类存储数据创建更有用的调试消息。我的代码看起来像这样

  #include< QAbstractTableModel> 
#include< QDebug>

/ **
*存储数据的模型。
* /
class DataModel:public QAbstractTableModel {
//用于调试目的
friend QDebug operator<< (QDebug d,const DataModel& model);

//其他东西
};

/ **
*用于调试目的的重载运算符
* /
QDebug运算符< (QDebug d,const DataModel& model){
d< 你好,世界!;
return d;
}

我期望 qDebug()<模型将打印Hello world!。然而,在输出上还有诸如QAbstractTableModel(0x1c7e520)之类的东西。



你有什么想法吗?

模型是指向DataModel和我的运算符的指针<< 只需要引用。


I'm trying to create more useful debug messages for my class where store data. My code is looking something like this

#include <QAbstractTableModel>
#include <QDebug>

/**
  * Model for storing data. 
  */
class DataModel : public QAbstractTableModel {
    // for debugging purposes
    friend QDebug operator<< (QDebug d, const DataModel &model);

    //other stuff
};

/**
  * Overloading operator for debugging purposes
  */
QDebug operator<< (QDebug d, const DataModel &model) {
    d << "Hello world!";
    return d;
}

I expect qDebug() << model will print "Hello world!". However, there is alway something like "QAbstractTableModel(0x1c7e520)" on the output.

Do you have any idea what's wrong?

解决方案

After an hour of playing with this question I figured out model is pointer to DataModel and my operator << takes only references.

这篇关于如何重载操作符&lt;&lt;为qDebug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆