[英]Adding data to CArray gives error “cannot access private member declared in class 'CObject'”
I have a structure called PERSONS
and then I have defined a type CTypedPtrArray<CPtrArray, PERSONS*> PersonsTable;
我有一个名为
PERSONS
的结构,然后我定义了一个类型CTypedPtrArray<CPtrArray, PERSONS*> PersonsTable;
struct Persons { ... };
typedef CTypedPtrArray<CPtrArray, PERSONS*> PersonsTable;
Later in the code I try to make a PersonTable
CArray
: 稍后在代码中我尝试创建一个
PersonTable
CArray
:
void CArrayTesting()
{
CArray<PersonsTable, PersonsTable> oCArr;
oCArr.SetSize(3);
PersonsTable perTableA;
PersonsTable perTableB;
PersonsTable perTableC;
for (int i = 0; i < 10; i++)
{
if (i < 3) addPerson(perTableA, i);
else if (i < 8) addPerson(perTableB, i);
else addPerson(perTableC, i);
}
oCArr.Add(perTableA);
oCArr.Add(perTableB);
oCArr.Add(perTableC);
for (int nArrCnt = 0; nArrCnt < oCArr.GetSize(); nArrCnt++)
for (int i = 0; i < oCArr.ElementAt(nArrCnt).GetSize(); i++)
{
cout<<"Table "<< nArrCnt << " - " << oCArr.ElementAt(nArrCnt)[i]->nID << endl;
delete oCArr.ElementAt(nArrCnt)[i];
}
}
The addPerson()
function adds data to PersonTable
. addPerson()
函数将数据添加到PersonTable
。
However, this causes an error during the compilation: cannot access private member declared in class 'CObject
. 但是,这会在编译期间导致错误:
cannot access private member declared in class 'CObject
。 The error appears to be related to oCArr.Add()
, because it gets 'fixed' when I delete the respected rows. 该错误似乎与
oCArr.Add()
,因为当我删除受尊重的行时它会被“修复”。
Why does the error appear? 为什么会出现错误? How can I fix it?
我该如何解决? And how does CArray work?
CArray是如何工作的?
The error boils down to class CPtrArray
not defining a copy constructor, which your code uses when adding to oCArr
. 错误归结为类
CPtrArray
未定义复制构造函数,您的代码在添加到oCArr
时使用。 The compiler generated default copy constructor will try to call the copy constructor for CObject
, which is declared private
. 编译器生成的默认复制构造函数将尝试为
CObject
调用复制构造函数,该对象被声明为private
。
You should probably add the elements into oCArr
directly, rather than adding them to local variables then making copies. 您可能应该直接将元素添加到
oCArr
,而不是将它们添加到局部变量然后进行复制。
1201 is correct, and it's a massive headache. 1201是正确的,这是一个巨大的头痛。 If you're forced to use the MFC collections, you could derive from them and create custom copy constructors/assignment operators.
如果您被迫使用MFC集合,则可以从它们派生并创建自定义复制构造函数/赋值运算符。 (and move + initializer_list) The array collections have a Copy function you can use to make life simple.
(和move + initializer_list)数组集合有一个复制功能,可以用来简化生活。
class CPtrArrayEx : public CPtrArray
{
public:
CPtrArrayEx() = default;
CPtrArrayEx(CPtrArrayEx const& rhs)
{
Copy(rhs);
}
CPtrArrayEx& operator=(CPtrArrayEx const& rhs)
{
if (this != &rhs)
Copy(rhs);
return *this;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.