Instantiating a template class

This is a discussion on Instantiating a template class within the C++ Programming forums, part of the General Programming Boards category; Heeeelllooo... ! Okay, I have this pretty weird problem, or so I think I have a CDLDAList Class, its declaration ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    Question Instantiating a template class

    Heeeelllooo... !


    Okay, I have this pretty weird problem, or so I think

    I have a CDLDAList Class, its declaration is:

    Code:
    template <class Type> class CDLDAList
    {
    private:
        /**
         * @brief Friend class declaration for CDLIterator
         */
        template <class Type> friend class CDLIterator;
    //..... declaration of private stuff
    public:
    
        /**
         * @brief    Default constructor
         * @pre        true
         * @post    Empty list constructed
         */
        CDLDAList<Type>();
    //.... rest of public thingies
    }
    and its constructor is defined as follows:

    Code:
    template <class Type> CDLDAList<Type>::CDLDAList<Type>()
    {
        nMaxIterators=100;
        nIterators=0;
        pHead=NULL;
        pTail=NULL;
        pIterators=new CDLIterator<Type>[nDeltaIterators];
        nItems=0;
    }
    now I have another class which has an attribute of type CDLDAList<int>, like this:

    Code:
    class CFacade
    {
    private:
        /**
         * @brief    List of Integers
         */
        CDLDAList<int> lList;
    //rest of class declaration
    }
    Now, trying to compile this, with two different compilers, gives different errors

    With Visual Studio .NET 2003 it gives me linker errors saying none of CDLDAList<int>'s member functions I'm using could be found by the linker

    Something among this line

    Quote Originally Posted by Visual Studio
    TallerListas1_09 error LNK2019: unresolved external symbol "public: __thiscall CDLDAList<int>::~CDLDAList<int>(void)" (??1?$CDLDAList@H@@QAE@XZ) referenced in function "public: __thiscall CFacade::~CFacade(void)" (??1CFacade@@QAE@XZ)

    TallerListas1_09 error LNK2019: unresolved external symbol "public: bool __thiscall CDLDAList<int>::eraseItem(class CDLIterator<int>)" (?eraseItem@?$CDLDAList@H@@QAE_NV?$CDLIterator@H@@ @Z) referenced in function "public: enum Result __thiscall CFacade::srvEraseItem(class CDLIterator<int>)" (?srvEraseItem@CFacade@@QAE?AW4Result@@V?$CDLItera tor@H@@@Z)
    and so on

    --------------------------

    Borland C++Builder 6.0 "stands" where I will make the text bold:

    template <class Type> CDLDAList<Type>::CDLDAList<Type>()
    {
    //constructor code here
    }

    saying

    Quote Originally Posted by Borland
    [C++ Error] DLDAList.cpp(14): E2040 Declaration terminated incorrectly
    Now, if I change it so it says template <class Type> CDLDAList<Type>::CDLDAList() instead, it will give me a linker error, just like MSVS.NET's, saying there's an unresolved external ( CDLDAList<int>::CDLDAList<int>() )

    I dunno if I'm not following proper syntax or what, but I'm pretty confused about what to do and I think I'm doing it how it's supposed to be done... I dunno


    Miguel Andrés
    Last edited by NullS; 02-23-2005 at 12:36 AM.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    i don't think the link errors are caused by the codes.
    plz show us more codes
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    What else should I post? I don't think it's suitable to post the whole source code for this on the site LOL

    Miguel

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    show us the destructor of CDLDAList and CFacade
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    Okay, i'm gonna post the doxygen documentation (with source codes)

    http://leela.uniandes.edu.co/~mi-yan...erlistas1docs/

    The destructor for the Facade is empty, but for the CDLDAList it is

    Code:
    template <class Type> CDLDAList<Type>::~CDLDAList<Type>()
    {
    	CNode<Type> *pTemp=pHead;
    	while (pHead!=NULL)
    	{
    		pTemp = pHead->getNext();
    		delete pHead;
    		pHead = pTemp;
    	}
    	if (pIterators!=NULL)
    		delete pIterators;
    }
    Miguel

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    is it that you can not contain the DLDAList.cpp in your project?
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    Nope, it's including perfectly, just templates won't work

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    The problem is that you have to define templated classes and such in your header or you will get linker errors. Just happened to my not to long ago.
    Woop?

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    Solved it myself, for reference, the solution was

    Include the .cpp's instead of the usual .h inclusion

    for example, Facade.h used to do
    #include "DLDAList.h"
    now it does
    #include "DLDAList.cpp"

    Thank you very much

    Miguel

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Yeh but if you're going to #include cpp files, you may as well stick the whole lot in the header, and have just one file.

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    My teacher would ........ing kill me for that :P

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Point him to the STL, Boost, and any other template library - they implement their classes in the headers. Putting the implementation in a cpp, then including the cpp is exactly the same thing, but you have 2 files rather than one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 10:42 PM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  5. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 06:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21