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

Author Topic: Sockets cannont be a vector  (Read 5235 times)

0 Members and 1 Guest are viewing this topic.

TestZombie

  • Newbie
  • *
  • Posts: 11
    • View Profile
Sockets cannont be a vector
« on: April 03, 2016, 01:38:36 am »
I dont know what to do
(click to show/hide)

this throws this error in vs2015

Severity   Code   Description   Project   File   Line   Suppression State
Error   C2280   'sf::TcpSocket::TcpSocket(const sf::TcpSocket &)': attempting to reference a deleted function   Test   c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0   655   

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Sockets cannont be a vector
« Reply #1 on: April 03, 2016, 10:25:13 am »
That error message is telling you that sf::TcpSocket lacks a copy constructor. Many SFML classes deliberately lack copy constructors because semantically it just doesn't make any sense to copy things like keyboards or mice or network connections (see the sf::NonCopyable documentation for a list of all the other non-copyable SFML classes).

The issue you've run into, which is a general C++ thing and not unique to SFML, is that std::vector can only store copyable types (or in C++11, moveable types). This is correct, because many of the typical std::vector operations require copying (or moving) the contained objects, so it would be wrong to let you compile this.

Depending on what you want to do with sockets, you can either use a raw array as your container, or you can have your container hold smart pointers to sf::TcpSockets instead of the sf::TcpSockets themselves.
« Last Edit: April 03, 2016, 10:27:22 am by Ixrec »

TestZombie

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Sockets cannont be a vector
« Reply #2 on: April 04, 2016, 05:03:07 am »
Thanks!

namosca

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Sockets cannont be a vector
« Reply #3 on: April 05, 2016, 10:50:32 pm »
Hey!

I dont know if you want a vector just because you want to store your objects in an organized way, or just because you want to resize the vector, but an std::list will work fine assuming your most wanted need is to store your objects in an organized way.

It just worked for me:

std::list<sf::TcpSocket> connectionList;
connectionList.emplace_back();// This creates a new socket, directly inside the list

For resizing, this looks dangerous to me to resize without knowing what will happen to the clients connected to your sockets, but this function is also avaiable in list. You can watch all a list can do here on this link:

http://www.cplusplus.com/reference/list/list/

Have a nice time!

« Last Edit: April 05, 2016, 11:11:45 pm by namosca »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Sockets cannont be a vector
« Reply #4 on: April 06, 2016, 09:03:48 pm »
For completeness, what namosca said is true of any "node-based container" (i.e. a container implemented with "nodes" that hold one item alongside pointers to other nodes). That includes std::list, std::map, std::set, and so on. These containers do not require the items they contain to be copyable because they never copy an item after it's been inserted, no matter what methods you call on them, which is why they work with non-copyable classes like sf::TcpSocket.

The downside of a node-based container is that it can't guarantee contiguous memory like a std::array or std::vector can, and there's the storage and runtime costs of chasing a bunch of pointers. But for many purposes you won't even notice that. It was silly of me not to mention this option in the last post.

 

anything