mirrol
mirrol

Reputation: 63

QT TCP socket connection exception. Too many arguments to function

Help, I looked everywhere where it is possible. If I create connections to the TCP server from the main class, then everything works. But if I create a separate class, it gives an error "Too many arguments to function".

Exception

enter image description here

Qt version 5.9.9 Using 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() {

}

Upvotes: 1

Views: 323

Answers (1)

The compiler found an ambiguity because it looks like you are trying to call the method

 void TcpClient::connect()

which is taking No args... so the solution is to "tell the compiler which method should be taken" i.e. you have to replace this:

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

with

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

with this modification the compiler is aware that the connect invocation you wrote is the one from the QObject class instead of the TcpClient

Upvotes: 2

Related Questions