QT TCP 套接字连接异常。函数参数太多
QT TCP socket connection exception. Too many arguments to function
帮助,我到处找可能的地方。如果我从主 class 创建到 TCP 服务器的连接,那么一切正常。但是如果我创建一个单独的 class,它会给出一个错误“函数参数太多”。
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() {
}
编译器发现了一个歧义,因为它看起来像你试图
调用方法
void TcpClient::connect()
没有参数...所以解决方案是“告诉编译器应该采用哪种方法”,即你必须替换它:
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));
和
QObject::connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));
通过此修改,编译器知道您编写的连接调用是来自 QObject
class 而不是 TcpClient
帮助,我到处找可能的地方。如果我从主 class 创建到 TCP 服务器的连接,那么一切正常。但是如果我创建一个单独的 class,它会给出一个错误“函数参数太多”。
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() {
}
编译器发现了一个歧义,因为它看起来像你试图 调用方法
void TcpClient::connect()
没有参数...所以解决方案是“告诉编译器应该采用哪种方法”,即你必须替换它:
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));
和
QObject::connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));
通过此修改,编译器知道您编写的连接调用是来自 QObject
class 而不是 TcpClient