Error C2280 mutex in a class C++ -
i'm having issue declaring mutex in class, whenever attempt instantiate class error c2280 appears. project create simulation of dining philosopher's problem.
declaration:
class fork { public: struct _position { float x; float y; }; fork(); fork(int num_phil_, int new_index_); _position returnworkposition(){ _position return_position_; /*position_mutex_.lock();*/ return_position_ = work_position_; /*position_mutex_.unlock();*/ return return_position_; }; float returnrotation(){ return rotation_; }; void calculateworkposition(int master_index_); void resetworkposition(){ /*position_mutex_.lock();*/ work_position_ = orig_position_; /*position_mutex_.unlock();*/ }; void takeownership(int master_index_); void relinquishownership(); private: int index_; int num_philosophers_; float rotation_; _position orig_position_; _position work_position_; //std::mutex master_mutex_; //std::mutex position_mutex_; };
instantiation:
for (int = 0; < num_philosophers_; i++) { fork temp_fork_(num_philosophers_, i); fork_.push_back(temp_fork_); }
edit:: new declaration:
class fork { public: fork(); fork(int num_phil_, int new_index_); fork(fork const&) = delete; fork& operator=(fork const&) = delete; struct _position { float x; float y; }; _position returnworkposition(){ _position return_position_; /*position_mutex_.lock();*/ return_position_ = work_position_; /*position_mutex_.unlock();*/ return return_position_; }; float returnrotation(){ return rotation_; }; void calculateworkposition(int master_index_); void resetworkposition(){ /*position_mutex_.lock();*/ work_position_ = orig_position_; /*position_mutex_.unlock();*/ }; void takeownership(int master_index_); void relinquishownership(); private: int index_; int num_philosophers_; float rotation_; _position orig_position_; _position work_position_; //std::mutex master_mutex_; //std::mutex position_mutex_; };
error messages:
error 18 error c2280: 'fork &fork::operator =(const fork &)' : attempting reference deleted function c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 2045 1 lab3e error 13 error c2280: 'fork::fork(const fork &)' : attempting reference deleted function c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 600 1 lab3e
new instantiation:
for (int = 0; < num_philosophers_; i++) { fork temp_fork_(num_philosophers_, i); fork_.emplace_back(temp_fork_); }
you need delete copy constructor , copy-assignment operator. cannot copy std::mutex
, therefore cannot copy instance of class, can construct or move one.
fork(fork const&) = delete; fork& operator=(fork const&) = delete;
the error telling you trying reference deleted function, copy constructor, because push_back
trying copy temp_fork_
. instead, use emplace_back
create fork
in-place.
for (int = 0; < num_philosophers_; i++) { fork_.emplace_back(num_philosophers_, i); }
or
for (int = 0; < num_philosophers_; i++) { fork temp_fork_(num_philosophers_, i); fork_.push_back(std::move(temp_fork_)); }
side note
instead of manually locking , unlocking std::mutex
, can using std::lock_guard
, class set handle locking/unlocking mutex via raii
void resetworkposition() { std::lock_guard<std::mutex> lock(position_mutex_); // position_mutex_ locked work_position_ = orig_position_; }; // lock fell out of scope, position_mutex_ unlocked _position returnworkposition() { _position return_position_; std::lock_guard<std::mutex> lock(position_mutex_); // position_mutex_ locked return_position_ = work_position_; return return_position_; };
Comments
Post a Comment