• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2416
  • Last Modified:

CArray::Add causes a compiler error

I'm having some issues with the CArray class.  It seems to be attempting to access a private member in CObject when I use the Add function.  The error I get is
error C2248: 'CObject::operator`='' : cannot access private member declared in class 'CObject'      
The error apparently occurs on line 529 of atlsimpstr.h, which is this function:
PCXSTR GetString() const throw()
      return( m_pszData );
Anyways, the relevant code from the program is as follows:

typedef struct tagITEMMAP
    DWORD Index;
    tagITEMMAP *Parent;
    CArray<tagITEMMAP, tagITEMMAP&> Children;

void Whatever(LPITEMMAP Item)
    ITEMMAP Temp;
    Temp.Parent = Item;
    Temp.Index = Entries[Item->Index].FirstIndex;

When I compile the program I get the following error:
error C2248: 'CObject::CObject': cannot access private member declared in class 'CObject'

This error is caused by the line Item->Children.Add(Temp);
since when it is commented out the program compiles and runs fine.
I am using the beta of Visual Studio 2005 (8.0.40607.16), but the error occurs in 2003 as well.

Thanks in advance for any help.

  • 3
  • 2
1 Solution
It seems to me that CArray is missing a =operator and not Temp but copy of it is tried to be added to the array. the copy of temp does make a shallow copy of CArray which should fail. Or have you supplied a =operator () for struct tagITEMMAP which knows how to copy the array?
The safer approach would be to have a CArray <LPITEMMAP, LPITEMMAP> which stores the pointer. Creation of the objects is now entirely up to you to code.
Your code requires copying of the whole ITEMMAP structure by Array.Add function. However, one of members of ITEMMAP is CArray. So, CArray instance must be copied using operator=, which is not allowed.
Your design looks strange: CArray is kind of list or variable-size array. Item kept in this array have CArray member - why? It looks like manually created linked list:

struct ListItem
    ListItem* next;

But you don't need this, CArray takes care about members order. What are you trying to do?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

DarkSnoopyAuthor Commented:
I intended to copy the whole Temp object when it's added because the actual code goes into a loop where it adds many items to Item->Children, all with the same value for parent.  Since the memory should already be allocated for the items by the SetSize function, I didn't want to use CArray <LPITEMMAP, LPITEMMAP> because it adds the extra hassle of taking care of memory allocation myself.  I'll try writing an =operator() for tagITEMMAP when I get a chance, but I'm in class right now... will be back in a couple hours and hopefully I can accept your answer.

AlexFM, the function is parsing through the directory map of a filesystem.  The value of Index is not the same as the index of the child, it is the index of the entry in the directory map, and the parent value is a pointer to the parent object in the filesystem.  The CArray is used so I can easily traverse the directory structure and only have to create the structure once instead of parsing the directory map several times.  The need for Index is to be able to access the file from the directory structure that is created.  If you can think of an easier way to do this that would avoid this mess that would be helpful too, but this is the easiest thing I could think to do.
You don't need to take care about memory management but you need to write the copy operator of a CArray which I doubt is simple and needing to copy the array content. When it is always empty as in this case this should not be a problem. Only if files get moved you will have problems.

But what is so difficult in the memory handling? In each call to add you need to have
LPITEMMAP Temp = new tagITEMMAP ();
Temp->Parent = Item;

And when destroying the list you need to delete all those items. But I think that way is much clearer.
DarkSnoopyAuthor Commented:
It worked excellently, thank you very much.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now