MFC CTypedPtrList in a CTypedPtrList error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: MFC CTypedPtrList in a CTypedPtrList error

  1. #1
    Join Date
    Oct 2011
    Posts
    2

    MFC CTypedPtrList in a CTypedPtrList error

    I need to store my data in a list of lists as shown in the code

    class CMyData
    {
    public:
    CString m_strStuff;
    };

    typedef CTypedPtrList< CPtrList, CMyData*> CMyDataList;
    typedef CTypedPtrList< CPtrList, CMyDataList*> CMyDataListList;

    I create a list and add a list to it

    CMyDataListList listMyData;
    CMyDataList *pList = new CMyDataList;
    pList->AddTail( new CMyData );
    listMyData.AddTail( pList );

    I get the error
    Error 1 error C2440: 'return' : cannot convert from 'void' to 'POSITION' c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxtempl.h 1961

    I can not see what is wrong, can nayone else? I can do the same with maps in maps or a list in an CTpedArray and all is OK.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,422

    Re: MFC CTypedPtrList in a CTypedPtrList error

    Quote Originally Posted by Baldric7202 View Post
    I can not see what is wrong, can nayone else?
    To see what is wrong, you need to go into the MFC code where the error is generated (go to the Output Window and click on the line where the error originates).

    It is an error because of this:
    Code:
    POSITION AddTail(TYPE newElement)
    	{ return BASE_CLASS::AddTail(newElement); }
    This is in afxtempl.h. In your case BASE_CLASS is a CPtrList. Now let's look at AddTail for CPtrList:
    Code:
    // add before head or after tail
    POSITION AddTail(void* newElement);
    void AddTail(CPtrList* pNewList);
    It is overloaded. The second overload is used if the item to add is a pointer to a CPtrList, or if the compiler finds that this overload is the best match for the function. This overload returns void, and that is where your problem starts.

    Note that return type is not considered by the compiler when given two overloaded functions and the compiler must choose which one is the best match. There is an overload version of AddTail in CTypedPtrList:
    Code:
    void AddTail(CTypedPtrList<BASE_CLASS, TYPE>* pNewList)
    		{ BASE_CLASS::AddTail(pNewList); }
    But again, return type is not considered when the compiler is doing its lookup for the best matching function. So the compiler doesn't match up "void" with "void", only that argument you're giving AddTail(). I won't even go through the maze of intermal MFC code as to why the compiler matches the POSITION AddTail() to the void AddTail() and gives the error, but that's the reason for the error.

    So either you set up your list's differently, or you can use std::list. To be honest with you, it looks like you're overcomplicating something that should be simple. What exactly are you trying to accomplish with all of these pointers? In this day and age of C++, coding like this is really not necessary.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 27th, 2011 at 04:15 PM.

  3. #3
    Join Date
    Oct 2011
    Posts
    2

    Re: MFC CTypedPtrList in a CTypedPtrList error

    Paul,

    Thanks for the reply, it confirms the conclusion I came to about an hour after I posted the question. In the end I have ended up with an CTypedPtrArray as the outer list, less efficient for what I need, I may change it to a simple CPtrList later. We use the CTypedPtrArray/List as we have derrived classes that automatically delete the objects when they are removed from the list.

    The reason for the 2 lists is because we have a list of rleated tasks passed from the front end or remote clients, these are added to a queue, the outer list, for the backend to handle.

    Mark

  4. #4
    Join Date
    Apr 2004
    Posts
    4

    Re: MFC CTypedPtrList in a CTypedPtrList error

    Quote Originally Posted by Baldric7202 View Post
    I need to store my data in a list of lists as shown in the code

    class CMyData
    {
    public:
    CString m_strStuff;
    };

    typedef CTypedPtrList< CPtrList, CMyData*> CMyDataList;
    typedef CTypedPtrList< CPtrList, CMyDataList*> CMyDataListList;

    I create a list and add a list to it

    CMyDataListList listMyData;
    CMyDataList *pList = new CMyDataList;
    pList->AddTail( new CMyData );
    listMyData.AddTail( pList );

    I get the error
    Error 1 error C2440: 'return' : cannot convert from 'void' to 'POSITION' c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxtempl.h 1961

    I can not see what is wrong, can nayone else? I can do the same with maps in maps or a list in an CTpedArray and all is OK.
    I stumbled on this today.

    You have two choices,
    1. listMyData.InsertAfter(list.MyData.GetTailPosition(), pList);
    2. ((CObList *) &listMyData)->AddTail((CObject *) pList);

    I lean toward the first choice.

    Anthony Wieser
    Wieser Software Ltd

  5. #5
    Join Date
    Apr 2004
    Posts
    4

    Re: MFC CTypedPtrList in a CTypedPtrList error

    You can improve option 1 (above) thus:
    listMyData.InsertAfter(NULL, pList);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center