求助 一个输出操作符重载的问题
直接上代码
customer.h
#ifndef _CUSTOMER_H#define _CUSTOMER_H#include <QString>#include <QTextStream>#include <QObject>class Customer:public QObject{ Q_OBJECTpublic: explicit Customer(const QString &name); QString toString() const; friend QTextStream& operator << (QTextStream &out, const Customer &c);};#endif
#include "customer.h"#include "customer.h"Customer::Customer(const QString &name){ setObjectName(name);}QString Customer:: toString() const{ QString retval = QString("Student ") + objectName(); return retval;}QTextStream& Customer::operator << (QTextStream &out, const Customer &c){ out << c.toString() ; return out;}
#include <QApplication>#include <QList>#include <QDebug>#include "customer.h"int main(){ QObject root; Customer *name1 = new Customer("a"); name1->setParent(&root); Customer *name2 = new Customer("b"); name2->setParent(&root); QObject *name3 = new QObject; name3->setObjectName("c"); name3->setParent(&root); QList<Customer *> list = root.findChildren<Customer*>(); foreach(const Customer *current ,list) { qDebug() << current->toString() << " "; } return 0;}