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

标签 c++ qt tcp

帮忙,我到处寻找可能的地方。如果我从主类创建到TCP服务器的连接,则一切正常。但是,如果我创建一个单独的类,它将给出错误“函数的参数过多”。
Exception
enter image description here
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() {

}

最佳答案

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

 void TcpClient::connect()
这没有参数...所以解决方案是“告诉编译器应该采用哪种方法”,即您必须替换为:
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));
QObject::connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));
经过这种修改,编译器知道您编写的连接调用是QObject类中的调用,而不是TcpClient

关于c++ - QT TCP套接字连接异常。实参太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62694579/

相关文章:

qt - QGraphicsItem 沿着路径移动

C++ Singleton类创建多个实例

http - 为什么我无法在对称 channel 上获得相同的上传和下载速度?

c++ - boost::asio 数据包顺序和连续性

c++ - C++中打印 double 的最大宽度

C++将 vector 的 vector 元素插入 vector

qt - QGraphicsItem::prepareGeometryChange() 是如何工作的?

sockets - Google 会为它收到的每个请求打开多少个套接字?

c++ - 测量执行时间——在程序代码中还是在 shell 中?

c++ - Eigen:如何防止大对象的额外拷贝;分配给结果而不实现 RHS 上的完整矩阵