locked
Using CTypedPtrList RRS feed

  • Question

  • I am encountering an interesting problem attempting to use the CTypedPtrList template.  I have a header where I define the class whose instance pointers will be contained on the list.  After the class is complete with };  I follow it with the follow two lines:

    typedef CTypedPtrList<CPtrList, Customer* > custlist;
    custlist Users;

    Customer is the class defined previously in the header file.  When I compile this, I get the following errors, which prevent Users from being defined:

    error C2143: missing ';' before '<'
    error C2146: missing ';' before identifier 'Users'
    error C2501: 'custlist' missing storage-class or type specifiers
    error C2501: 'Users' missing storage-class or type specifiers

    All the online and manual references I have seem to indicate that I am using this template properly, so I'm not sure what the problem is.  Any help would be appreciated.

    Thursday, July 15, 2010 3:00 PM

Answers

  • You are probably missing a #include. The following compiles for me (in an MFC project):

    class Customer{};

    typedef CTypedPtrList<CPtrList, Customer* > custlist;
    custlist Users;


    http://blog.voidnish.com
    Thursday, July 15, 2010 12:30 PM

All replies

  • Thank you, that did the trick.  I should have known that - I've been teaching VB too long!

    I've nearly got the project done.  I've getting only one more error, when using the template class CTypedPtrList.  I have the following:

    typedef CTypedPtrList<CPtrList, Customer* > custlist;
    custlist Users;

    I get the C2143 error: missing ';' before '<' twice, then get C2146 error: missing ';' before identifier Users

    Every reference I can see about using CTypedPrtList says that my two lines are the correct way to do it, so I'm confused.  I need to use CTypedPtrList - I need to have pointers in the linked list, not references.  How can I finish this project?

    Thursday, July 15, 2010 3:00 AM
  • You are probably missing a #include. The following compiles for me (in an MFC project):

    class Customer{};

    typedef CTypedPtrList<CPtrList, Customer* > custlist;
    custlist Users;


    http://blog.voidnish.com
    Thursday, July 15, 2010 12:30 PM
  • On Thu, 15 Jul 2010 15:00:29 +0000, Prof Robert Ekblaw wrote:

    I am encountering an interesting problem attempting to use the CTypedPtrList template.  I have a header where I define the class whose instance pointers will be contained on the list.  After the class is complete with };  I follow it with the follow two lines:

    typedef CTypedPtrList<CPtrList, Customer* > custlist;
    custlist Users;

    Customer is the class defined previously in the header file.  When I compile this, I get the following errors, which prevent Users from being defined:

    error C2143: missing ';' before '<'
    error C2146: missing ';' before identifier 'Users'
    error C2501: 'custlist' missing storage-class or type specifiers
    error C2501: 'Users' missing storage-class or type specifiers

    All the online and manual references I have seem to indicate that I am using this template properly, so I'm not sure what the problem is.  Any help would be appreciated.

    Drop the typedef. It's not needed.

    Steve


    Neural Planner Software Ltd                  www.NPSL1.com
    EasyNN-plus. Neural Networks plus.           www.easynn.com
    SwingNN.     Forecast with Neural Networks.  www.swingnn.com
    JustNN.      Just Neural Networks.           www.justnn.com


    npsl1
    Thursday, July 15, 2010 3:13 PM
  • Didn't work.  I still get the C2143 and C2501 error, except now the C2501 error refers to CTypedPtrList: missing storage-class or type specifiers.
    Thursday, July 15, 2010 3:39 PM
  • On Thu, 15 Jul 2010 15:39:00 +0000, Prof Robert Ekblaw wrote:

    Didn't work.  I still get the C2143 and C2501 error, except now the C2501 error refers to CTypedPtrList: missing storage-class or type specifiers.

    Have you included afxtempl.h ?

    Steve


    Neural Planner Software Ltd                  www.NPSL1.com
    EasyNN-plus. Neural Networks plus.           www.easynn.com
    SwingNN.     Forecast with Neural Networks.  www.swingnn.com
    JustNN.      Just Neural Networks.           www.justnn.com


    npsl1
    Thursday, July 15, 2010 3:44 PM
  • That did it!  The <afxtempl.h> file was being included with the <afx.h> block in StdAfx.h, but when I added the #include <afxtempl.h> after <afx.h> in StdAfx.h, it compiled.

    Thursday, July 15, 2010 3:58 PM
  • Those MFC containers like CTypedPtrList seems to me belonging to the "Middle Age" of C++.

    In modern C++ I'd suggest using robust STL containers, like std::list.

    typedef std::list< Customer * > CustomerList;
    
    

    Note that thanks to smart pointer template classes like shared_ptr, you can have a form of "deterministic garbage collector", i.e. if you use store shared_ptr<Customer> instead of raw Customer pointer in the list, you don't have to waste brain cycles to track the life of the pointed object; C++ destructors, STL containers and shared_ptr will take care of that for you, and they will release the allocated object as soon as it is no more referenced.

    Giovanni

     

    Friday, July 16, 2010 2:34 PM