943,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 232
  • C++ RSS
Sep 3rd, 2010
0

static cast Problem , help plz :)

Expand Post »
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 :

C++ Syntax (Toggle Plain Text)
  1. #include "server.h"
  2. #include "ui_server.h"
  3. #include <QtNetwork/QTcpSocket>
  4. Server::Server(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::Server)
  7. {
  8. ui->setupUi(this);
  9. tcpServer = new QTcpServer(this);
  10. tcpServer->listen(QHostAddress::Any,quint16(7676));
  11. connect(tcpServer,SIGNAL(newConnection()),this,SLOT(Client()));
  12. }
  13.  
  14. Server::~Server()
  15. {
  16. delete ui;
  17. tcpServer->close();
  18. }
  19.  
  20. void Server::Log(QString toLog)
  21. {
  22. ui->plainTextEdit->appendPlainText(toLog+"\n");
  23. }
  24.  
  25. void Server::ClientDisconnected()
  26. {
  27. QString msg = "Disconnected Client : ";
  28. QTcpSocket *tmp = static_cast<QTcpSocket*>(sender); //PROBLEM HERE
  29. msg += tmp->peerAddress().toString();
  30. Log(msg);
  31. clients.removeAll(tmp);
  32. }
  33.  
  34. void Server::BroadcastMessege(QString msg)
  35. {
  36. for(int i=0;i<clients.length();i++)
  37. {
  38. QTextStream stream(clients[i]);
  39. stream << msg << endl;
  40. }
  41. }
  42.  
  43. void Server::ReadClient()
  44. {
  45. QTcpSocket* tmp = static_cast<QTcpSocket*>(sender); // PROBLEM HERE
  46. QString readdata;
  47. while(tmp->canReadLine())
  48. readdata += tmp->readLine();
  49. Log("Messege : "+readdata);
  50. }
  51.  
  52. void Server::Client()
  53. {
  54. QString msg = "New Connection From : ";
  55. QTcpSocket *ss = tcpServer->nextPendingConnection();
  56. QHostAddress cAd = ss->peerAddress();
  57. msg += cAd.toString();
  58. Log(msg);
  59. clients.append(ss);
  60. connect(ss,SIGNAL(disconnected()),this,SLOT(ClientDisconnected()));
  61. connect(ss,SIGNAL(readyRead()),this,SLOT(ReadClient()));
  62. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
medopunsher is offline Offline
26 posts
since Jul 2009
Sep 3rd, 2010
0
Re: static cast Problem , help plz :)
I think you are looking for reinterpret_cast<>?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,859 posts
since Dec 2008
Sep 3rd, 2010
0
Re: static cast Problem , help plz :)
Solved , i forget to define sender
Thanks 4 ur help anyway
Last edited by medopunsher; Sep 3rd, 2010 at 1:16 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
medopunsher is offline Offline
26 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: video/image editing
Next Thread in C++ Forum Timeline: Insert Graphics.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC