Error using CArray

So, I am trying to use CArrayas follows:

 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:

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

I don’t even understand where this comes from.

HELP!

+4
source share
6 answers

The error you are getting is that you are trying to use CArrayas return value from what I can collect. If you change it by returning the CArrayvalue of the reference parameter instead, this will be compiled.

Try the following:

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

- Copy CObject? (CArray CObject)

:

 CArray<CPerson,CPerson&> allPersons;  

//do something

// This gives the error C2248, cannot access Copy constructor of CObject.
CArray<CPerson,CPerson&> aTemp = allPersons;

?

CArray<CPerson,CPerson&> allPersons; 
...
CArray<CPerson,CPerson&> aTemp;

//Error, as Assignment operator is private
aTemp = allPersons;

: CArray, CopyArray() .

CopyArray(sourceArray, DestArray&)
{
 for each element in SourceArray
 add the element to DestArray.
}
+2

CArray<CPerson> allPersons;? , , ...

0

CPerson CObject? private? SetAtGrow() .

, Add(), , , SetAtGrow().

0

, : Microsoft CObject.

, :

class Person
{
    // ...
    Person( const Person& src );
}

Person::Person( const Person& src ){ Person();*this = src; }

, .

0

CPerson - , , pointers

   CArray<CPerson*,CPerson*> allPersons;
   int i=0;
   for(int i=0;i<10;i++)
      allPersons.SetAtGrow(i,new CPerson(i));

,

   for(int i=0;i<allPersons.GetSize();i++)
      delete allPersons.GetAt(i);
0

All Articles