【问题标题】:QT TCP socket connection exception. Too many arguments to functionQT TCP 套接字连接异常。太多参数无法运行
【发布时间】:2020-10-22 22:46:01
【问题描述】:

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

Exception

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() {

}

【问题讨论】:

    标签: c++ qt tcp


    【解决方案1】:

    编译器发现了一个歧义,因为看起来您正在尝试 调用方法

     void TcpClient::connect()
    

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

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

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

    通过此修改,编译器知道您编写的连接调用来自QObject 类而不是TcpClient

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 2015-05-18
      • 2021-09-10
      • 1970-01-01
      • 2011-08-31
      • 2023-03-21
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多