试图传递CStringArray会导致错误无法访问类' CObject'中声明的私有成员。

时间:2014-11-21 00:35:30

标签: c++ visual-studio-2013 mfc compiler-errors cstring

我收到一个奇怪的错误,告诉我我无法访问在课堂上声明的私人成员' CObject'当简单地尝试将CS​​tringArray传递给我编写的函数时,将其分解为碎片。 我已经注释掉了我的整个功能代码,所以我知道问题存在于对象本身的传递中,我假设我做错了。

这是我的代码:

    // 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:&#39; CObject :: CObject&#39; :无法访问类中声明的私有成员&gt;&#39; CObject&#39; c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ atlmfc \ include \ afxcoll.h 590 1&gt; SDI-1

1 个答案:

答案 0 :(得分:5)

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

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

void CStringArrayHandler(const CStringArray& arr);