-
Hi, I'm working on a Client App and I wish to create a Client class where I create 2 threads (R and S), who execute functions Receiver and Sender, respectively...
class Client {
sf::IPAddress ServerAddress;
sf::SocketTCP Socket;
sf::Packet Packet;
sf::Thread R, S;
void Receiver(void*);
void Sender(void*);
bool connected;
bool updated;
public:
Client();
sf::Packet Receive();
void Send(sf::Packet);
bool IsConnected();
bool IsUpToDate();
};
Then, I get:
Thread.hpp|92|error: 'sf::Thread::Thread()' is protected|
I tried with
class Client : public sf::Thread {
and
class Client : private sf::Thread {
but this doesn't resolve it
How can I resolve it?
PD: Sorry if bad english ^^
-
There's a tutorial for sf::Thread. Have you read it?
-
Yes, I readed it and I know how to create a Thread and launch it. I can use Thread with a function; and I can use Thread as class... but now, I wish use Thread with fuction on my class Client. Is it possible?
In brief, I need a class with 2 differentes threads, but when I declare sf::Thread on a class, I get:
Thread.hpp|92|error: 'sf::Thread::Thread()' is protected|
-
but now, I wish use Thread with fuction on my class Client. Is it possible?
No, a member functions is not a non-member function. They are two totally different things.
In brief, I need a class with 2 differentes threads
This is impossible in SFML 1.6. You should switch to 2.0.
-
Ok, I am on SFML 2.0 now.
And now I get this error:
error: no matching function for call to 'sf::Thread::Thread()'|
note: candidates are: sf::Thread::Thread(const sf::Thread&)|
I can't declare sf::Thread without the constructor, and I can't construct on my class Client... how I should do it?
-
Why can't you construct your sf::Thread instance in the owner class constructor?
-
Like this?
Client::Client()
{
sf::Thread R(&Receiver);
cout << "Client running" << endl;
ServerAddress ="**.**.**.**";
Socket.connect(ServerAddress, *****);
cout << "Connected" << endl;
}
I get the same error :-\
-
No, you must use the initialization list (http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/).
-
Could you give me a example with sf::Thread please? :-[
-
class Client
{
public:
Client() : m_thread(&Client::R, this)
{
}
private:
void R()
{
...
}
sf::Thread m_thread;
}
I'll add an example in the tutorial, I just noticed that there's nothing about "using sf::Thread in a class".
-
It's works! :D Thanks you very much.
There is my code:
Client.hpp
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
class Client
{
sf::IpAddress ServerAddress;
sf::TcpSocket Socket;
sf::Packet Packet;
void Receiver();
void Sender();
bool connected;
bool updated;
public:
Client();
sf::Thread R, S;
sf::Packet Receive();
void Send(sf::Packet);
bool IsConnected();
bool IsUpToDate();
};
Client.cpp
#include "Client.hpp"
Client::Client() : R(&Client::Receiver, this), S(&Client::Sender, this)
{
cout << "Client is running!" << endl;
ServerAddress ="**.**.**.**";
Socket.connect(ServerAddress, *****);
cout << "Client connected!" << endl;
R.launch();
S.launch();
}
void Client::Receiver() //Thread
{
cout << "Receiver here!\n";
while(1)
{
sf::Packet buffer;
Socket.receive(buffer);
cout << "Received!\n";
Packet = buffer;
buffer.clear();
updated=false;
}
}
void Client::Sender() //Useless Thread Yet (xD)
{
cout << "Sender here!\n";
}
sf::Packet Client::Receive()
{
updated=true;
return Packet;
}
void Client::Send(sf::Packet Pack)
{
connected = (Socket.send(Pack) == sf::Socket::Done);
}
bool Client::IsUpToDate()
{
return updated;
}
bool Client::IsConnected()
{
return connected;
}