There are modest three lines of the plus code written in VS 2015

std::list <sf::TcpSocket> soc; sf::TcpSocket co; soc.push_back(co); 

At the assembly stage, the following error occurs

error C2280: "sf :: TcpSocket :: TcpSocket (const sf :: TcpSocket &)": an attempt was made to reference a remote function

I see such a mistake for the first time, I didn’t come across VS in earlier versions, I found the error code on MSDN, but I didn’t see anything intelligible there. What kind of beast and how to fight?

  • one
    and what is sf::TcpSocket - your class? It seems to have no default constructor. - KoVadim
  • No, not my class, the default constructor is - Jereli
  • one
    as mentioned below, not only the default constructor is needed there, but also the copies. I think you need the usual shared_ptr. - KoVadim

2 answers 2

The sf :: TcpSocket class has no copy constructor. Are you sure you need to store objects, not pointers?

 std::list<sf::TcpSocket*> soc; sf::TcpSocket *co = new sf::TcpSocket(); soc.push_back(co); 

If sf :: TcpSocket is your class and you are sure that it is the class instances that are to be stored in the list, not the pointers to them. then you need to define the copy constructor:

 sf::TcpSocket::TcpSocket(const sf::TcpSocket &other) { //тут копируем нужные нам объекты } 
  • It is necessary to store objects, and not pointers to them. Because The code looks like this. while (running) {// Te 3 lines) Ie with each iteration, the object will be deleted, in case I place pointers, they will refer to the object that was already deleted. - Jereli
  • Why will they refer to a remote object? if it is created through new, and delete is not called, nothing will be deleted. - KoVadim
  • Now I’ll ask you a completely shameful question, will it be deleted correctly in the future? - Jereli
  • one
    @Jereli I described exactly how to solve your compilation problem. If the class sf :: TcpSocket is implemented by you, then it is up to you to decide which logic it will use. But usually, the TcpSocket class (networking) does not imply the ability to copy, which occurs when the item is placed in std :: list. Therefore, it would be more logical to place pointers on the list. and, of course, do not forget to delete them correctly when necessary. - Alexander
  • one
    в дальнейшем удаление из списка произойдет корректно - I do not see any problems with deletion. Unless try to delete the same object twice. - KoVadim

Starting with c++11 types stored in std::vector do not require the presence of a copy constructor. In this case, however, will need to move.

In the case of your sf::TcpSocket this, of course, does not help (judging by the description ), but in certain situations it can be useful.

 #include <iostream> #include <vector> struct S { S() { std::cout << "def ctor\n"; } S(const S&) = delete; S(S&&) { std::cout << "mv ctor\n"; } void f() const { std::cout << "f()\n"; } }; int main() { std::vector<S> v; v.push_back(S{}); for(auto& s: v) { sf(); } } 

Result of performance