Hey everybody ,
I'am writing a very simple chat program (Client & Server) in C++ & QTNetwork libary.
I Have a static cast problem that i don't understand , here is my code :

PS.Lines with errors are commented :

#include "server.h"
#include "ui_server.h"
#include <QtNetwork/QTcpSocket>
Server::Server(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Server)
{
    ui->setupUi(this);
    tcpServer = new QTcpServer(this);
    tcpServer->listen(QHostAddress::Any,quint16(7676));
    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(Client()));
}

Server::~Server()
{
    delete ui;
    tcpServer->close();
}

void Server::Log(QString toLog)
{
    ui->plainTextEdit->appendPlainText(toLog+"\n");
}

void Server::ClientDisconnected()
{
    QString msg = "Disconnected Client : ";
    QTcpSocket *tmp = static_cast<QTcpSocket*>(sender); //PROBLEM HERE
    msg += tmp->peerAddress().toString();
    Log(msg);
    clients.removeAll(tmp);
}

void Server::BroadcastMessege(QString msg)
{
    for(int i=0;i<clients.length();i++)
    {
        QTextStream stream(clients[i]);
        stream << msg << endl;
    }
}

void Server::ReadClient()
{
    QTcpSocket* tmp = static_cast<QTcpSocket*>(sender); // PROBLEM HERE
    QString readdata;
    while(tmp->canReadLine())
    readdata += tmp->readLine();
    Log("Messege : "+readdata);
}

void Server::Client()
{
    QString msg = "New Connection From : ";
    QTcpSocket *ss = tcpServer->nextPendingConnection();
    QHostAddress cAd = ss->peerAddress();
    msg += cAd.toString();
    Log(msg);
    clients.append(ss);
    connect(ss,SIGNAL(disconnected()),this,SLOT(ClientDisconnected()));
    connect(ss,SIGNAL(readyRead()),this,SLOT(ReadClient()));
}

The error code is the same for both problems , here it is :

error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘QTcpSocket*’

Any help would be greatly appreciated.
Thanks. :)

Solved , i forget to define sender :D
Thanks 4 ur help anyway :)

Edited 6 Years Ago by medopunsher: n/a

This question has already been answered. Start a new discussion instead.