[英]How to create “CArray<CArray<CString>>& results” in MFC?
I'm an unable to create a 2D array in MFC as per the code "CArray>& results". 根据代码“ CArray>&results”,我无法在MFC中创建2D数组。 Code: 码:
CArray<CArray<CString>> res;
CArray<CString>strArray1;
strArray1.Add(L"Ali");
strArray1.Add(L"Ahmed");
strArray1.Add(L"Mark");
CArray<CString>strArray2;
strArray2.Add(L"1");
strArray2.Add(L"2");
strArray2.Add(L"3");
res.Add(strArray1);
res.Add(strArray2);
Error after execution: error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' 执行后发生错误:错误C2248:'CObject :: operator =':无法访问在类'CObject'中声明的私有成员
This should be done without overriding copy and equals method in CArray, instead is there any way where I wouldn't need those indirectly like some method of CArray that can be leveraged. 应该在不覆盖CArray中的copy and equals方法的情况下完成此操作,相反,有什么方法可以像使用CArray的某些方法那样间接地不需要那些方法。
The signature for CArray::Add()
is CArray::Add()
的签名是
INT_PTR Add(ARG_TYPE newElement);
You'll notice that the newElement
argument is passed by value. 您会注意到newElement
参数是按值传递的。 This means the element type must be copy-constructible, which CObject
s are not. 这意味着元素类型必须是可复制构造的,而CObject
则不是。 This isn't particularly well-documented; 没有特别有据可查的文件; parts of the CMap
documentation state that the value type of the map has to be copy-constructible, so we can assume the rest of the API was designed similarly. CMap
文档的某些部分指出,映射的值类型必须是可复制构造的,因此我们可以假定其余API的设计都是类似的。
This question explains why CObject
s are not copy-constructible. 这个问题解释了为什么CObject
不可复制构造。
So what can you do? 所以,你可以做什么? You have several options. 您有几种选择。
CArray<CArray<CString> *>
— store the inner dimension arrays as pointers instead of values; 切换为使用CArray<CArray<CString> *>
-将内部维数组存储为指针而不是值; this also saves memory 这也节省了内存 CTypedPtrArray<CObjArray, CArray<CString> *>
, which allows you to use CObArray
instead, and still be type-safe 切换到使用CTypedPtrArray<CObjArray, CArray<CString> *>
,这使您可以改用CObArray
,并且仍然是类型安全的 std::vector<std::vector<CString> >
as @PaulMcKenzie suggested above. 切换到使用标准C ++容器,即@PaulMcKenzie上面建议的std::vector<std::vector<CString> >
。 CString
is not a CObject
, so you can use it directly. CString
不是CObject
,因此您可以直接使用它。 Alternatively, if you are just using CString
as a wrapper around C strings, you can also switch to std::string
or std::wstring
, but only do this if you know what you are doing. 另外,如果只是将CString
用作C字符串的包装器,则还可以切换到std::string
或std::wstring
,但是只有在知道自己在做什么的情况下,才执行此操作。 CArray
, you can also do std::vector<CArray<CString> *>
. 当然,如果需要内部尺寸为CArray
,则还可以执行std::vector<CArray<CString> *>
。 As usual, the pattern requires you to use pointers — you can't just say std::vector<CArray<CString> >
for the same reason as above ( std::vector
requires copy-constructibility). 与往常一样,该模式要求您使用指针-出于与上述相同的原因,您不能只说std::vector<CArray<CString> >
( std::vector
需要复制可构造性)。 CArray<CString>
or std::vector<CString>
or whatever) of size m * n
(where m
is the size of the inner dimension). 使用大小为m * n
(其中m
为内部维度的大小)的一维数组( CArray<CString>
或std::vector<CString>
或其他)。 In that case, arr[i][j]
in your code snippet is the same as arr[i * m + j]
. 在这种情况下,您的代码段中的arr[i][j]
与arr[i * m + j]
。 In fact, this is what multidimensional arrays boil down to, as textbook examples on matrix multiplication in C will show. 实际上,这就是多维数组的基础,正如有关C中矩阵乘法的教科书示例所示。 (Thanks to @IInspectable for reminding me of this one.) (感谢@IInspectable提醒我这一点。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.