Solved

updating to use templates

Posted on 2006-11-08
9
224 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find boost header files using gnu make (MinGW) on Win 7 11 79
C++ Language error 28 197
C++ question 3 62
How to remove Recent Projects from Embarcadero C++ builder XE10. Berlin 2 32
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

896 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

14 Experts available now in Live!

Get 1:1 Help Now