gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:00:44 24 4
gpt4 key购买 nike

帮忙,我到处寻找可能的地方。如果我从主类创建到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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com