CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Template function

  1. #1
    Join Date
    Jan 2004
    Location
    Ireland
    Posts
    34

    Template function

    Hello there,

    My application has several different kinds of linked lists, of which all are created dynamically using "new".

    I wish to create a template function that will free up the memory when finished with the linked lists.

    Is it possible to create a template function that will take different pointers to structures (ie, nodes). And what is the format for it??

    Thanks in advance.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401
    You should make a template for your linked list class. Any C++ book/tutorial should have a section on that.

    You should also ask yourself why to make a template for lists, if there is already one in the standard C++ library (called list) which you can simply use.

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    I'm not sure what are you looking for, but you can always:
    template <class T1, class T2, ...>
    void foo(T1 *p1, T2 *p2, ...)
    {
    //whatever stuff.
    }

  4. #4
    Join Date
    Jan 2004
    Location
    Ireland
    Posts
    34
    AdDav,


    I have a template function as follows:

    template <class Type> void Delete(Type * list)
    {
    //do something
    }


    and its prototype in the header file is:

    template <class Type> Delete(Type);


    And I call the function as follows:


    Delete(list_head);//list head is a pointer to a structure


    I get an unresolved external:

    int __cdecl Delete(struct V_FSM *)" (?Delete@@YAHPAUV_FSM@@@Z)


    Whats wrong with this code?

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    What's wrong with std::list<>?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Jan 2004
    Location
    Ireland
    Posts
    34
    I have alot of coding done using my own lists. I did not know about the std::list.

  7. #7
    Join Date
    Jan 2004
    Posts
    20
    Originally posted by Fergal21
    AdDav,


    I have a template function as follows:

    template <class Type> void Delete(Type * list)
    {
    //do something
    }


    and its prototype in the header file is:

    template <class Type> Delete(Type);


    And I call the function as follows:


    Delete(list_head);//list head is a pointer to a structure


    I get an unresolved external:

    int __cdecl Delete(struct V_FSM *)" (?Delete@@YAHPAUV_FSM@@@Z)


    Whats wrong with this code?
    1. unless it's a typo, your prototype doesn't match the implementation - the argument has to be a pointer.

    2. template code must reside in the header file - so you probably want to replace the header declaration of the function with the actual definition. this is the usual reason for the unresolved external error.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)