[SOLVED]QList and QTcpSocket*



  • Hi, today I'm trying to write simple multithreaded client/server program, which will save connected client's sockets to QList and then send them automatically some messages. I've done something like this:

    global_objects.h
    [code]#ifndef GLOBAL_OBJECTS_H
    #define GLOBAL_OBJECTS_H
    #include <QList>
    #include <QTcpSocket>

    QList <QTcpSocket*> Clients;

    #endif // GLOBAL_OBJECTS_H[/code]

    server.cpp
    [code]void Server::incomingConnection(int handle)
    {
    foreach(QTcpSocket pSocket, Clients) //can't convert from const int to QTcpSocket
    {
    qDebug() << pSocket;
    }

    mThread *thread = new mThread(handle);
    thread->run();
    

    }[/code]

    I'm always getting this error. When I tried for loop, like this:

    [code]for(int i = 0; i < Clients.size(); i++) {
    Clients.at(i)->socketDescriptor(); //error, -> must point union class or whatever.
    }[/code]

    I don't get it, honestly. Somebody explain me please!



  • Try using this code:

    @
    for(int i = 0; i < Clients.size(); i++)
    {
    (Clients[i])->socketDescriptor();
    }
    @

    You should post the whole error message because it's difficult to help you with this information.

    And another thing: Some compilers don't like variables in loop definitions like this:
    @
    for(int i; ...
    @

    You should declare it outside the loop to keep it portable.

    On the other hand - this code is possible:
    @
    for(...)
    {
    int i;
    ...
    }
    @



  • Thanks, solved it.


  • Moderators

    Please be sure and edit the title of the original post to add [Solved]. Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.