HelpIterators/Functions.

Posts 1–3 of 3 · Page 1 of 1
Iterators/Functions.

[Request close, Solved]
I am currently coding a basic server using Qt (Why Qt? Because. Its not important anyway) In one of my functions called when someone connects to my server, there is this part of code :

Code:
for(vector<DBase*>::iterator it = clients.begin(); it != clients.end() ; ++it) 
        {
            if((*it)->getSocket() == socket)
            {
                (*it)->setName(_switch);
                logBox->append("[" + QString((*it)->getName()) + "] Connecté");
                break;  //On arrete le if pour accélérer;
            }
        }
(_switch is a char*)

DBase is a class I made which stores the name of the client and a pointer to his socket on my server. Everything works fine in this part of the code.
The name is stored using the method setName() and showed using the method getName(). They both work good.

Here are the two functions used in this statement

Code:
char* getName()
        {
            return nom_client;
        }
        void setName(char* _name)
        {
            nom_client = _name;
        }
Later in the code, when someone is disconnecting, I have a problem.

Code:
for(vector<DBase*>::iterator it = clients.begin(); it != clients.end() ; ++it) 
        {
            if((*it)->getSocket() == socket && (*it)->getSocket()!= NULL && socket != NULL)   
            {
                _name = QString((*it)->getName()); // _name != Erreur
                break; 
            }
        }

logBox->append("[Déconnexion Client] " + _name);
socket is a pointer to the client who's disconnecting. Now I try the if statement, it works fine. My problem is that the function append(), instead of showing the name of the socket, always shows the same string "I>m" or something like that. Now I don't understand, because the function getName() worked in the first function, and those are the only two times I use them oO

Does anyone see an error?
I'd imagine it was here:

Code:
"[Déconnexion Client] " + _name
"[Déconnexion Client]" is a char*, the + operator for a char* doesn't concatenate strings together. You need to make it a QString where they've overloaded the + operator.

I.e:

Code:
logBox->append(QString("[Déconnexion Client] ") + _name);
If that doesn't fix the error there may be something wrong with your log.
Quote Originally Posted by Broderick View Post
I'd imagine it was here:

Code:
"[Déconnexion Client] " + _name
"[Déconnexion Client]" is a char*, the + operator for a char* doesn't concatenate strings together. You need to make it a QString where they've overloaded the + operator.

I.e:

Code:
logBox->append(QString("[Déconnexion Client] ") + _name);
If that doesn't fix the error there may be something wrong with your log.
It was a dangling pointer(the source of the problem was posted outside of these code snippets. I helped gab on MSN). QString overloads the + operator so that you can use it with a char pointer.
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us