0

I am at the start of building a berkeley simulation in c++. I keep getting this error and I don't understand it's meaning. I looked it up on the internet and it says there are problems if I got no default constructor. But I have one in all classes. This problem occurs when I add the channel variable in TimeSlave. Can please someone help?

The error is:

error: use of deleted function ‘TimeSlave::TimeSlave(TimeSlave&&)’
  : _M_head_impl(std::forward<_UHead>(__h)) { }

And there is a note which says that the copy-constructor is deleted implicitly because the default would be ill formatted...

Class TimeSlave:

class TimeSlave{
    Clock clock;
    Channel channel;
public:
    TimeSlave(string name, int hours, int minutes, int seconds) : clock{name, hours, minutes, seconds} {}
    void operator()(){
        clock();
    }
    Channel* get_channel(){
        return &channel;
    }
};

Class Channel:

class Channel{
    Pipe<long> pipe1;
    Pipe<long> pipe2;

public:
    Channel(){}
    Pipe<long>& get_pipe1(){
        return pipe1;
    }

    Pipe<long>& get_pipe2(){
        return pipe2;
    }
};

Class Pipe:

template <typename T>
class Pipe {
    std::queue<T> backend;
    std::mutex mtx;
    std::condition_variable not_empty;
    bool closed{false};
  public:
    Pipe<T>(){}
    Pipe& operator<<(T value) {
        if(closed) return *this;
        lock_guard<mutex> lg{mtx};
        backend.push(value);
        not_empty.notify_one();
        return *this;
    }

    Pipe& operator>>(T& value) {
        if(closed) return *this;
        unique_lock<mutex> ulck{mtx};
        not_empty.wait(ulck, [this](){ return backend.get_size() == 0; });
        return *this;
    }

    void close() {
        closed = true;
    }

    explicit operator bool() {
        return !closed;
    }
};
S.Babovic
  • 335
  • 5
  • 15
  • 2
    Mutexes cannot be copied: http://en.cppreference.com/w/cpp/thread/mutex/mutex –  Jan 04 '18 at 22:57
  • 2
    `TimeSlave` contains a `Channel` and a `Channel` contains a `Pipe` and a `Pipe` contains a `std::mutex` and a `std::mutex` cannot be copied because multiple copies of a mutex would defeat the point of a mutex. – user4581301 Jan 04 '18 at 22:57
  • Oh my god.. thank you! – S.Babovic Jan 04 '18 at 22:59
  • In addition, to what has already been said: technically, the error is about move-constructor, but the problem is the same. – Algirdas Preidžius Jan 04 '18 at 23:02
  • @AlgirdasPreidžius good point. But a `std::mutex` doesn't have a move constructor either. There's a question on that I read a while back. Lemme go fishing. – user4581301 Jan 04 '18 at 23:04
  • Here we go: [Move constructor for std::mutex](https://stackoverflow.com/questions/7557179/move-constructor-for-stdmutex) – user4581301 Jan 04 '18 at 23:04
  • @user4581301 Yep, I know. That's why I said "_but the problem is the same_". :) – Algirdas Preidžius Jan 04 '18 at 23:06
  • @AlgirdasPreidžius My apologies. On re-read I see your point. – user4581301 Jan 04 '18 at 23:10
  • 1
    Cheap hack solution: `std::unique_ptr mtx;`. Example: https://ideone.com/0D7A2E . There has to be a better way around this, but I'm not seeing one. – user4581301 Jan 04 '18 at 23:11
  • I couldn't see it, but apparently Howard Hinnant could, so Possible duplicate of [How should I deal with mutexes in movable types in C++?](https://stackoverflow.com/questions/29986208/how-should-i-deal-with-mutexes-in-movable-types-in-c) – user4581301 Jan 04 '18 at 23:43

0 Answers0