尝试传递 CStringArray 给出错误无法访问在类"CObject"中声明的私有成员

Trying to pass a CStringArray gives error cannot access private member declared in class 'CObject'

本文关键字:CObject 声明 成员 访问 CStringArray 出错 错误      更新时间:2023-10-16

我收到一个奇怪的错误,告诉我当只是尝试将CStringArray传递给我编写的函数以将其分解为碎片时,我无法访问在类"CObject"中声明的私有成员。我已经注释掉了我的整个函数代码,所以我知道问题在于对象本身的传递,我假设我这样做不正确。

这是我的代码:

    // If successful, read file into CStringArray
    CString strLine;
    CStringArray lines;
    while (theFile.ReadString(strLine))
    {
        lines.Add(strLine);
    }
    // Close the file, don't need it anymore
    theFile.Close();
    // Break up the string array and separate it into data
    CStringArrayHandler(lines);

这是我的CStringArrayHandler函数:

void CSDI1View::CStringArrayHandler(CStringArray arr)
{
    // Left out code here since it is not the cause of the problem
}

这是我的头文件中函数的声明:

class CSDI1View : public CView
{
// Operations
public:
    void CStringArrayHandler(CStringArray arr);   // <<<<===================

这是我得到的错误全文:

错误

1 错误 C2248:"CObject::CObject":无法访问类>"CObject"中声明的私有成员 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\ATLMFC\include\afxcoll.h 590 1>SDI-1

您正在按值传递CStringArray arr,因此CStringArray的复制构造函数必须是可访问的。但事实并非如此,因为CStringArray继承自禁止复制的CObject(这就是编译器错误消息,您实际上没有在此处完全粘贴)所说的)

解决方案是通过引用传递arr

void CStringArrayHandler(const CStringArray& arr);