SFML community forums

Help => System => Topic started by: ggggggggggggggg on May 02, 2013, 05:36:04 pm

Title: Thread gives no copy even when given a reference.
Post by: ggggggggggggggg on May 02, 2013, 05:36:04 pm
The function I'm trying to push into a thread:

void Send(sf::UdpSocket& newSocket, unsigned short& newPort, sf::IpAddress& newTargetIP, sf::Packet& newPackage)
{
        string input;
        cin >> input;
        newPackage << input;

        if(newSocket.send(newPackage, newTargetIP, newPort) != sf::Socket::Done)
                cout << "Failed to send message." << endl;
        else
                cout << "You: " << input;

}

The thread construction:
sf::Thread Send(std::bind(&Send, socket, port, target, packet));

Error it gives:
'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
This diagnostic occurred in the compiler generated function 'sf::Socket::Socket(const sf::Socket &)'

I know you can't copy sockets so I passed it though a reference, right? Sorry if it's an obvious answer.
Title: Re: Thread gives no copy even when given a reference.
Post by: Nexus on May 02, 2013, 05:42:47 pm
Use std::ref() and std::cref() to bind references.
Title: Re: Thread gives no copy even when given a reference.
Post by: ggggggggggggggg on May 02, 2013, 05:55:54 pm
Like this?

sf::Thread Send(std::cref(&Send, socket, port, target, packet));

It says it doesn't take its arguments.
Title: Re: Thread gives no copy even when given a reference.
Post by: Nexus on May 02, 2013, 05:59:53 pm
No. There are pages like www.cppreference.com where you can lookup standard library functionality ;)
Title: Re: Thread gives no copy even when given a reference.
Post by: Hnoss on May 06, 2013, 01:24:19 am
Hi, I'm trying to do something similar and I have the same error.

I tried to do
sf::Thread send(std::bind(&send, ref(socket));

according to what I found in http://en.cppreference.com/w/cpp/utility/functional/bind (http://en.cppreference.com/w/cpp/utility/functional/bind)

but it didn't work saying that I'm still copying the socket (the same error).

I have no clue of what to do right now.
Title: Re: Thread gives no copy even when given a reference.
Post by: Nexus on May 06, 2013, 11:55:28 am
Its std::ref (since you use the namespace qualifier also for std::bind).

Otherwise, come up with a complete and minimal example.
Title: Re: Thread gives no copy even when given a reference.
Post by: Hnoss on May 06, 2013, 12:53:07 pm
Indeed I made a mistake in the post, I'm sorry.

I actually did write
void send(sf::TcpSocket client)

instead of
void send(sf::TcpSocket &client)
in the prototype.
I did not make that mistake for the receive function though.

Thank you Nexus and asusralis, you helped me to finish my first project using SFML (the basic principle of a chat).