error C2280: 'std::mutex::mutex(const std::mutex &)' : attempting to reference a deleted function

清泛原创

std::mutex是noncopyable的结构,因此不存在拷贝构造函数,所以这里错误提示引用已经删除的函数。

错误示例代码如下:

 

解决方法:

     将包含std::mutex的类的拷贝构造函数和赋值操作符重载函数,自定义或者标记为delete.

例如:

class Account {
public:
	Account(int id_, double ba = 0.0) :id(id_), balance(ba){}

	void withdraw(double amount){
		balance -= amount;
	}

	void deposit(double amount){
		balance += amount;
	}
	void printInfo() const {
		std::cout << "Account id: " << id << " balance: " << balance << std::endl;
	}
	Account(const Account& other) = delete;
	Account& operator=(const Account& other) = delete;
	friend void transfer(Account& from, Account& to, double amount);
private:
	double balance;
	int id;
	std::mutex m;
};

error C2280 mutex

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部