c++ - 使用 CArray 时出错

标签 c++ mfc

所以,我正在尝试像这样使用 CArray:

 CArray<CPerson,CPerson&> allPersons;
   int i=0;
   for(int i=0;i<10;i++)
   {
      allPersons.SetAtGrow(i,CPerson(i));
      i++;
   }

但是在编译我的程序时,我得到了这个错误:

"error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxtempl.h"

我什至不明白这是从哪里来的。

帮助!

最佳答案

你得到的错误是因为你试图使用 CArray 作为我可以收集到的返回值。如果您将它从返回 CArray 更改为采用引用参数,那么它将编译。

试试这个:

class CPerson
{
public:
    CPerson();
    CPerson(int i);
    void operator=(const CPerson& p) {}
private:
    char* m_strName;
};

CPerson::CPerson()
{}

CPerson::CPerson(int i)
{
    sprintf(m_strName,"%d",i);
}

void aFunction(CArray<CPerson,CPerson&> &allPersons)
{
    for(int i=0;i<10;i++)
    {
        allPersons.SetAtGrow(i,CPerson(i));
        i++;
    }
}

关于c++ - 使用 CArray 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/864864/

相关文章:

vs2017中MFC开发

c++ - 如何创建自定义组件并将其添加到基于对话框的应用程序 (MFC)?

c++ - 在 MFC 中创建 CPaneDialog 时断言失败

c++ - gtest : check if string is equal to one of two strings

C++ 和 python : TypeError resolution for vector arguments ? 。

c++ - C++中的递归函数调用

c++ - 派生到基础的转换和友元困惑

c++ - Visual Studio MFC C++ "CFormView"从 "base class"下拉列表中丢失

c++ - 根据其他变量设置变量

c++ - StrRetToBuf 函数的链接器错误