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!!

using namespace std;
template<class T>
class node
    T data;
    node * link;
    friend class ll;

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

Recommended Answers

All 2 Replies

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() {}

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:


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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.