Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Thread gives no copy even when given a reference.  (Read 4111 times)

0 Members and 1 Guest are viewing this topic.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Thread gives no copy even when given a reference.
« 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thread gives no copy even when given a reference.
« Reply #1 on: May 02, 2013, 05:42:47 pm »
Use std::ref() and std::cref() to bind references.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: Thread gives no copy even when given a reference.
« Reply #2 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thread gives no copy even when given a reference.
« Reply #3 on: May 02, 2013, 05:59:53 pm »
No. There are pages like www.cppreference.com where you can lookup standard library functionality ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hnoss

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Thread gives no copy even when given a reference.
« Reply #4 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

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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Thread gives no copy even when given a reference.
« Reply #5 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hnoss

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Thread gives no copy even when given a reference.
« Reply #6 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).