C ++使用类中的原子成员的赋值运算符获取错误

时间:2017-10-12 13:25:30

标签: c++ variable-assignment atomic assignment-operator stdatomic

这是一个简单的代码来显示我的问题。

#include <iostream>
#include <atomic>

using namespace std;

class T {
public:
atomic<int> a;
};

int main() {
int x;
T X,Y;
x = 10;
X.a = x;
Y = X;
cout << Y.a;
}

运行时出现以下错误消息:

test.cpp: In function ‘int main()’:

test.cpp:16:5: error: use of deleted function ‘T& T::operator=(const T&)’

Y = X;
     ^

test.cpp:6:7: note: ‘T& T::operator=(const T&)’ is implicitly deleted 
because the default definition would be ill-formed:

 class T {
       ^

test.cpp:6:7: error: use of deleted function ‘std::atomic<int>& std::atomic<int>::operator=(const std::atomic<int>&)’

In file included from test.cpp:2:0:

/usr/include/c++/6/atomic:617:15: note: declared here
       atomic& operator=(const atomic&) = delete;

0 个答案:

没有答案