tags:

views:

437

answers:

4

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 :

"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"

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

HELP !

A: 

Do you mean to say CArray<CPerson> allPersons; ? I don't know how leaving out the contained type would lead to the reported error, but...

Mark Ransom
Attilah
The second parameter is optional, if omitted, it will be the same as the first parameter.
crashmstr
A: 
crashmstr
CArray was templated but it was not properly visible. Pls see the edited version.
aJ
i tried your suggestion but the same error occurs again.
Attilah
this is my aFunction : can the problem be related to it ?CFeed<CFeed,CFeed int i=0; for(int i=0;i<10;i++) { allPersons.SetAtGrow(i,CPerson(i)); i++; }return allPerson;}
Attilah
yep. That is the problem. The CArray cannot be returned like that it seems.
crashmstr
That is the problem. Your function aFunction() tries to call copy constructor of CArray and it is defined private in CObject. Please refer to my answer
aJ
aJ
I don't think Attilah wants a copy per se, but an initialized array.
crashmstr
crashmstr,i did that and it works.Thanks a lot.
Attilah
Your welcome. Good luck with the rest of the project.
crashmstr
crashmstr,thanks.man, C++/MFC can really make me look older in less than a month.i'm used to C#/.NET. this seems so difficult sometimes.
Attilah
crashmstr
A: 

Is CPerson derived from CObject? Does it have a private constructor? Your use of SetAtGrow() seems correct to me otherwise.

If that doesn't work, you might try falling back to using the Add() function, as your loop doesn't seem to require SetAtGrow().

mwigdahl
tried using Add() function, the error remains.
Attilah
A: 

Are you using any of the Copy constructor or assignment operator of CObject ? ( CArray is derived from CObject)

For instance:

 CArray<CPerson,CPerson&> allPersons;  

//do something

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

OR

Are you doing this?

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

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

EDIT: If you want to copy the elements in CArray, write a helper method CopyArray() and copy the elements manually.

CopyArray(sourceArray, DestArray&)
{
 for each element in SourceArray
 add the element to DestArray.
}
aJ
no, i didn't do any of the things you cited. i just did this :CArray<CPerson,CPerson int i=0; for(int i=0;i<10;i++) { allPersons.SetAtGrow(i,CPerson(i)); i++; }
Attilah
This code looks correct. Can you post CPerson definition. By any chance CPerson derived from CObject?
aJ
//person.hclass CPerson{ public: CPerson(); CPerson(int i); private: char* m_strName;}//person.cppCPerson::CPerson(){}CPerson::CPerson(int i){ sprintf(m_strName,"%d",i);}
Attilah
I think it has to do with the fact that CArray tries to call the constructor of CObject but that constructor is private.How to circumvent this ?
Attilah
I just compiled what you had, and it works (with syntax fixes)
crashmstr
CObject constructor is protected and protected constructors are allowed for base classes. CArray is derived from CObject.
aJ
crashmstr
Yeah, copy can be used. Or pass the Array as input param by reference.
aJ