Solved

updating to use templates

Posted on 2006-11-08
9
214 Views
Last Modified: 2010-04-01
Hi,

I wrote an implementation for a doubly linked list, but now I want to update it a bit to use a generic type instead of a concrete storage class. So I have something like (pseudo code):

    class CPerson {
         string m_strName;
         string m_strTelephoneNumber;
    }

    class CLinkedList {
        CPerson *m_pList; // start of the list of nodes.

         int FindNode(string strName)
         {
             // go through all nodes and compare names of nodes to passed name to find a match.
             // ...
         }
    }

Now first I don't know the syntax for replacing the list variable with a generic type. Second problem is, even if I do that, how could I replace the FindNode function, since it is specifically written for a CPerson type node, comparing a string member? Wouldn't I now need some kind of 'generic' find function?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 5
  • 4
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17903388
Basically, that would be

    template<typename TNode>
    class CLinkedList {
        TNode *m_pList; // start of the list of nodes.

         int FindNode(string strName)
         {
             // go through all nodes and compare names of nodes to passed name to find a match.
             // ...
         }
    };

>>Wouldn't I now need some kind of 'generic' find function?

Yes, but you can only ensure that by either coercing the node type to be derived form a certain base class or simply to force it to expose a certain member.

I'd suggest to take a look at the tutorial at http://www.cplusplus.com/doc/tutorial/templates.html ("Templates") for the big picture, that explains it better
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 17903606
Hi jkr,

OK I am starting to understand this better. I thought of making an intermediate Node class which just stores the data as whatever type the user wants:

    template <class T>
    class CNode {
         public:
             CNode(T data)
             {
                 m_data = data;
             }
       
            T m_data; // user data, so this class just wraps around it.

            CNode *m_pNext, *m_pLast;
    }

Ok now I have the interface type class the user would actually interact with, but the compiler is complaining about needing a template argument list for all the member functions:

    class CLinkedList {

        CNode *m_pNodes;
    }

I kind of need that CNode class cause it would let me have the backward and forward pointers, and still let me keep user defined data in there too. But it seems like CLinkedList does not want to compile now since CNode is a template class?

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 17903624
>>But it seems like CLinkedList does not want to compile now since CNode is a template class?

Yes, you need to specify the data type for the list also, e.g.

    template<typename T>
    class CLinkedList {

        CNode<T> *m_pNodes;
    }
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 17903645
Hmm ok but if I do that now for every function in CLinkedList I have to do:

     template<typename T>
     void CLinkedList::DoSomething(CNode<T> &node)
     {
     }

and also throw them all in the header right?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:jkr
ID: 17903666
Yup, but that's the price you have to pay ;o)

However, in the header, that is only neede with the class declaration, not on each member (excpet for the parameter types, of course).
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 17903703
Ok, this should be my last question on this black magic. I want to use my interface class like this from main():

    int main()
    {
        CLinkedList<CWhatever> list;
        list.AddNode(CWhatever("junk"));
    }

Now in the CLinkedList interface, I declared AddNode() like:

    void CLinkedList::AddNode(T t)
    {
         m_pNodes = new CNode<T>(t);
    }

and a CNode constructor is defined like:

    template <class T>
    class CNode {

         public:
            CNode<T>(T data)
            {
                m_data = data;
            }
     };

now the compiler is saying no appropriate constructor available in CNode for my call to new in AddNode() - what should the syntax for that be? I just want to allocate a new CNode and pass through the user type for the data.

Thanks for your help
0
 
LVL 86

Expert Comment

by:jkr
ID: 17903756
Just add a default constructor - without knowing the whole code, it is hard to tell what the compiler is choking on. E.g.

    template <class T>
    class CNode {
         public:
             CNode() // default constructor
             {
                 m_pNext = NULL; m_pLast = NULL;
             }
             CNode(T data)
             {
                 m_data = data;
             }
       
            T m_data; // user data, so this class just wraps around it.

            CNode *m_pNext, *m_pLast;
    }
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 17903796
Ok it seems to be working, the error was that the random class type I was using in the container didn't have the default constructor defined which I guess it needs for copying or something?

    CWhatever() {};

once I put that in it was fine. I have a simpler question on this stuff which I'll open in a new post if you want to take a look, thanks for your help, this stuff is a bit confusing starting out.
0
 
LVL 86

Expert Comment

by:jkr
ID: 17903824
Thanks!

>>this stuff is a bit confusing starting out.

That I won't object ;o)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 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

16 Experts available now in Live!

Get 1:1 Help Now