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. :)

I think you are looking for reinterpret_cast<>?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.