CArray::Add causes a compiler error

Posted on 2004-11-18
Last Modified: 2013-11-20
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.

Question by:DarkSnoopy
    LVL 13

    Expert Comment

    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?
    LVL 13

    Expert Comment

    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.
    LVL 48

    Expert Comment

    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?
    LVL 1

    Author Comment

    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.
    LVL 13

    Accepted Solution

    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.
    LVL 1

    Author Comment

    It worked excellently, thank you very much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now