I have a linked list class i'd like to turn into a template so that it can handle strings,float,double,etc

I currently wrote a simple linked list class that has a node that holds the link and a string. I'd like to know practice templates. What i'd like to do is anywhere there was a string in my code i'd like to make it eneric so it can hold int,double,etc....

I'm thinking i should be able to just template the functions that handle the string parameter. Anyone who can help me learn this would be great i'll show my old code then below it i'll show how i tried to change it with comments any tips would be appreciated.

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I understand your difficulties; some of the syntax can be a little confusing. The keyword 'class' in template<class A_Type> does not mean class in the usual C++ terminology. A better keyword which performs the exact same function is 'typename'. From MSDN, the following two definitions are identical:
template< class T1, class T2 > class X...
template< typename T1, typename T2 > class X...

'A_Type' can be whatever you want it to be. It is the name, or placeholder, for the type upon which you are templating. You state that your class has a single templated type parameter, which you will call A_Type. Whenever A_Type is referred to, it means the type the user chose when instantiating it (creating an instance of the template class based on a specific type). It could be called anything, T is quite common.

In your implementation, just as you have to state which class member functions belong to by scoping them, eg. Node::Node(), so you must state the template and type parameters, like this:

void setName(A_Type data, Node *node); <== you said there is a problem here so
void template<A_Type>::setName(A_Type data, Node *node);<== do i need to define scope here? it just seems i'd do that in .cpp..is that what i need to do in node.h

Ok, one thing unclear in the way most if not all people lay out template code is that they split the definition onto two lines. The part in Node.h conceptually reads:

template<class A_Type> class Node { // ... };

This defines Node as a template class with one type parameter called 'A_Type'. Everything within the braces defining class Node is a part of that template.
Now, in the implementation file, where you would usually have to specify e.g. void Node::Display(), because it is templated, you must now write

template<class A_Type> void Node<A_Type>::Display()

As well as having to specify that Display() is a member function of Node by scoping: Node::Display(), you must also restate that the function is part of a templated class, that way you may then refer to the type parameter A_Type by name.

I hope that helps, if not I will rephrase. If you need to post any more code snippets, please try to cut them down to the relevant parts; it's difficult to trawl through screenfuls of code to find a particular comment.
--Rob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.