使用 CArray 时出错

2022-01-12 00:00:00 c++ mfc

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

So, I am trying to use CArray like this :

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

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

But when compiling my program, I get this error :

"错误 C2248: 'CObject::CObject' :无法访问声明的私有成员在CObject"类中 c:program文件微软视觉工作室9.0vcatlmfcincludeafxtempl.h"

"error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' c:program filesmicrosoft visual studio 9.0vcatlmfcincludeafxtempl.h"

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

I don't even understand where this is coming from.

帮助!

推荐答案

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

The error you are getting is because you are trying to use a CArray as a return value from what I can gather. If you change it from returning a CArray to taking a reference parameter instead, that will compile.

试试这个:

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++;
    }
}

相关文章