5

Windows SOCKET のリストをサポートするために構造体を使用しています。

struct ConnectedSockets{
    std::mutex lock;
    std::list<SOCKET> sockets;
};

これをコンパイルしようとすると (Visual Studio 2012)、次のエラーが発生します。

「エラー C2248:std::mutex::operator =クラスで宣言された 'private' メンバーにアクセスできません'std::mutex'

これを修正する方法を知っている人はいますか?

4

2 に答える 2

6

Aはコピーできないため、自分std::mutexで実装する必要があります。operator=ConnectedScokets

mutexのインスタンスごとに保持したいConnectedSocketsので、これで十分だと思います:

ConnectedSockets& operator =( ConnectedSockets const& other )
{
    // keep my own mutex
    sockets = other.sockets; // maybe sync this?

    return *this;
}
于 2013-01-10T17:37:45.810 に答える
2

根本的な原因はソケットについて何もありません。std::mutexはコピーできないため、コンパイラはstructのデフォルトの代入演算子(メンバー単位)の生成に失敗しますConnectedSockets。代入演算子を削除するようにコンパイラーに指示するか(= delete指定子を使用して宣言する)、またはミューテックスのコピーを無視するなど、手動で実装する必要があります。

// To make ConnectedSockets explicitly non-copyable and get more reasonable
// compiler error in case of misuse
struct ConnectedSockets
{
   std::mutex lock; 
   std::list<SOCKET> sockets;
   ConnectedSockets& operator=(const ConnectedSockets&) = delete;
};

// To make ConnectedSockets copyable but keep mutex member intact
struct ConnectedSockets
{
   std::mutex lock; 
   std::list<SOCKET> sockets;

   ConnectedSockets& operator=(const ConnectedSockets& i_rhs)
   {
      // Need locking? Looks like it can be problem here because
      // mutex:lock() is not const and locking i_rhs wouldn't be so easy
      sockets = i_rhs.sockets;
      return *this;
   }
};
于 2013-01-10T17:44:44.227 に答える