[英]error C2248: 'X::operator =' : cannot access private member declared in class 'X'
Similar questions are already asked on stackoverflow. 关于stackoverflow已经提出了类似的问题。 I do have looked at them but still unable to resolve the issue I am facing 我确实看过它们,但仍然无法解决我面临的问题
I have a structure X with few member variables including a mutex . 我有一个结构X ,成员变量很少,包括互斥体 。 I am using this mutex to lock access to member vector called vecIds when pushing elements in it from multiple threads. 当从多个线程中推入其中的元素时,我正在使用此互斥锁来锁定对称为vecIds的成员向量的访问。
Further, as I donot want objects of type X to be copyable or copy constructable, I declare copy constructor and copy assignment operator functions as private 此外,由于我不希望X类型的对象可复制或可复制构造,因此我将复制构造函数和复制赋值运算符函数声明为私有
struct X
{
bool a;
unsigned value;
std::vector<unsigned> vecIds;
std::mutex mutex;
X(): a(false), value(0), mutex(){}
private:
X( X const &x);
X& operator=( Xconst &x);
};
Somewhere in my code I am creating vector of 100 objects of type X. When I compile my code I see the following errors: 在我的代码中的某个地方,我正在创建100个类型为X的对象的向量。在编译代码时,我看到以下错误:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xutility(2466): error C2248: 'X::operator =' : cannot access private member declared in class 'X'
X.h(12): see declaration of 'X::operator ='
X.h(2): see declaration of 'X'
I donot seem to figure out where is the problem 我似乎不知道问题出在哪里
This is a VS2012 bug fixed in VS2013. 这是VS2013中修复的VS2012错误。 VS2012's implementation of VS2012的实现
explicit vector(size_type n);
calls resize()
, which requires the element to be both DefaultInsertable
and MoveInsertable
. 调用resize()
,这要求元素同时为DefaultInsertable
和MoveInsertable
。 This is nonconforming, since the constructor itself only requires DefaultInsertable
* . 这是不合格的,因为构造函数本身仅需要DefaultInsertable
* 。 (To make it worse, its resize()
implementation calls erase()
, which requires the type to be MoveAssignable
...) (更糟糕的是,其resize()
实现调用了erase()
,这要求类型为MoveAssignable
...。)
It's mentioned in this MSDN blog post (search for " vector<DefaultConstructible>(10)
"). 在此MSDN博客文章中提到了此问题 (搜索“ vector<DefaultConstructible>(10)
”)。
If you are using a fixed-length container, consider using a std::array<X, 100>
. 如果您使用固定长度的容器,请考虑使用std::array<X, 100>
。
* When the default allocator is used, the *Insertable
requirements basically translate to *Constructible
. *使用默认分配器时, *Insertable
需求基本上转换为*Constructible
。
Your code compiles fine in VS2013, to resolve your problem in a quick way, try a vector of smart pointers 您的代码在VS2013中可以正常编译,要快速解决问题,请尝试使用智能指针向量
#include <memory>
std::vector<std::unique<X>> x(100);
Note: your struct X has std::mutex as member, mutex is not copyable nor moveable, which means X is nonCopyable nor moveable. 注意:您的结构X具有std :: mutex作为成员,互斥体不可复制或不可移动,这意味着X不可复制或不可移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.