I'm new to generic programming and I just cant figure out what is wrong in this . The program is supposed to make a linked list using class.
I have used 2 classes so that a single object of class LL can be used to manipulate a linked list . I want the program to work for both integers and characters . When I try without the generic part , the code is working perfectly !
Somebody please help me ! My exam is tomorrow!!

#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class node
{
    T data;
    node * link;
    friend class ll;
    public:

};
  template<class T>
  class ll
{
    node * <T>start;
    public:
    template<class T>
    ll()
    {
        <T>start=NULL;
    }
    //void search();
    void add();
    void disp();
   //void remove();
};
  template<class T>
  void ll::add()
  {
      T x;
      node * <T>temp=new node;
      cout<<"\nData : ";
      cin>>x;
      temp->data=x;
      temp->link=start;
      start =temp;
  }
  template<class T>
   void ll::disp()
   {
       node *<T>ptr;
       T x;
       ptr =start;
       while(ptr!=NULL)
       {
           x=ptr->data;
           cout<<x<<"\n";
           ptr=ptr->link;
       }
   }
   int main()
   {
       ll <int>list1;
       int ch;
       while(1)
       {
           cout<<"\n1.Add\n2.Display\n3.Exit\nEnter choice : ";
           cin>>ch;
           switch(ch)
            {
                case 1  : list1.add();      break;
                case 2  : list1.disp();     break;
                case 3  : exit(0);    break;
                default : cout<<"Invalid Entry "; break;
            }
      }
     return 0;
   }

Any time you refer to a class that is written in a generic fashion, you need to provide a template argument(s) (the part between the "< >").

Your node pointers aren't declared properly. You need template arguments to indicate the proper type. Is is a pointer to a node conaining an int or a pointer to a node containing a char? i.e.

node * next;

should be:

node<T> *next;

Also, you need template arguments for your class names when resolving them for the function implementations. i.e.

template <typename T>
void ll::someFunc() {}

should be

template <typename T>
void ll<T>::someFunc() {}

Edited 5 Years Ago by Fbody: n/a

Like Fbody said.

Line 8 should be:

node<T>* link;

Line 16 should be:

node<T>* start;

Line 18 should be taken out.
Line 21 should be:

start=NULL;

Line 29 should be:

void ll<T>::add()

Line 32 should be:

node<T>* temp = new node<T>;

Line 42 should be:

node<T>* ptr;
This article has been dead for over six months. Start a new discussion instead.