我编写了一个客户端GUI来接收来自服务器的结构。编译时,我收到错误:
错误:“
中的“operator>>
”is >> newstruct.basestruct::element1
”不匹配
你能指出我出错的地方吗?
#include "dialog.h"
#include "ui_dialog.h"
#include <QString>
#include <QDebug>
#include <QTextStream>
#include <QDataStream>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->pushButton->setText("Connect");
ui->pushButton_2->setText("Ok");
ui->pushButton_3->setText("Close");
ui->pushButton_4->setText("Disconnect");
}
Dialog::~Dialog()
{
delete ui;
}
QDataStream& operator >>(QDataStream& is,const basestruct& newstruct)
{
is >> newstruct.element1;
qDebug()<<newstruct.element1;
}
void Dialog::Read()
{
socket->waitForReadyRead(-1);
QDataStream receiveData(socket);
receiveData.setVersion(QDataStream::Qt_4_0);
receiveData >>newstruct ;
}
void Dialog::on_pushButton_clicked()
{
socket=new QTcpSocket(this);
socket->connectToHost("127.0.0.1",5000);
if (socket->waitForConnected(-1))
qDebug()<<"Connected";
Read();
}
void Dialog::on_pushButton_4_clicked()
{
socket->close();
qDebug()<<"Disconnected";
}
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QTcpSocket>
namespace Ui {
class Dialog;
}
struct basestruct
{
int element1;
int element2;
};
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
void Read();
private slots:
void on_pushButton_clicked();
void on_pushButton_4_clicked();
private:
Ui::Dialog *ui;
QTcpSocket *socket;
friend Q_CORE_EXPORT QDataStream &operator >>(QDataStream&,const struct basestruct&);
struct basestruct newstruct;
};
#endif // DIALOG_H