QT TCP套接字连接异常。实参太多

用户名

帮忙,我到处寻找可能的地方。如果我从主类创建到TCP服务器的连接,则一切正常。但是,如果我创建一个单独的类,它将给出错误“函数的参数过多”。

例外

在此处输入图片说明

Qt版本5.9.9使用QTcpSocket

tcpclient.h

#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QObject>
#include <QTcpSocket>
#include <QDataStream>
#include <QHostAddress>

class TcpClient : public QObject {

public:
    TcpClient(QObject *parent = 0);

private:
    QTcpSocket *tcpSocket;

    const QString ip = "185.137.235.92";
    const int port = 9080;

public slots:
    void connect();

private slots:
    void onReadyRead();
    void onConnected();
    void onDisconnected();
};

#endif // TCPCLIENT_H

tcpclient.cpp

#include "tcpclient.h"

TcpClient::TcpClient(QObject *parent) :QObject(parent) {
    tcpSocket = new QTcpSocket(this);

    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));
    connect(tcpSocket, SIGNAL(connected()), this, SLOT(onConnected()));
    connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
}

void TcpClient::connect() {
    tcpSocket->connectToHost(QHostAddress(ip), port);
}

void TcpClient::onReadyRead() {

}

void TcpClient::onConnected() {
    if(tcpSocket->waitForConnected() ) {
        QDataStream in(tcpSocket);
        ushort id;
        uint size;
        quint8 type;
        ushort action;
        QString message;

        in >> id >> size >> type >> action;

        message  = tcpSocket->read(size);

        qDebug() << id;
        qDebug() << size;
        qDebug() << type;
        qDebug() << action;
        qDebug() << message;
    }
}

void TcpClient::onDisconnected() {

}
ΦXocę웃Пepeúpatsu

编译器发现模棱两可,因为您似乎正在尝试调用该方法

 void TcpClient::connect()

这没有参数...所以解决方案是“告诉编译器应该采用哪种方法”,即您必须替换为:

connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));

QObject::connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));

经过这种修改,编译器意识到您编写的连接调用是来自QObject该类的,而不是TcpClient

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章