Thread: help !!concept of class templates

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    17

    help !!concept of class templates

    I have tried using a class template concept

    but it does not seem to work ,
    throws 2 error statements on compilation

    i am new to the concept of class templates

    what is the error in my program ???

    /////
    Error 663: "templ2.cpp", line 15 # Expected template arguments following template name 'student'.
    void student::add()
    ^^^^^^^
    Error 553: "templ2.cpp", line 31 # Expected type arguments to follow class template name.
    student s1;





    Code:
    #include<iostream.h>
    template <class T>
    class student
    {
    
       T rollno;
       T mark1;
       T mark2;
    
       public:
        void  add();  /* function prototype */
    
    };
    template <class T>
    void student::add()
    {
              T total;
              cout<<"enter the rollno of the student";
              cin>>rollno ;
              cout<<"enter the mark in the first subject ";
              cin>>mark1;
              cout<<"enter the mark in the second subject ";
              cin>>mark2;
    
              total = mark1 + mark2;
              cout<<" the total in the two subjects is "<<total;
    
              cout<<" the roll no is "<<rollno;
    
    };
    
    int main()
    {
      student s1;
      s1.add();
    
      return 0;
    
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    template <class T>
    void student::add()
    This should be
    Code:
    template <class T>
    void student<T>::add()
    Also, when declaring an instance of this class it should not be like this:
    Code:
    student s1;
    but this:
    Code:
    student<int> s1;
    int, or whatever datatype you want T to be.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class templates and iterators
    By creativeinspira in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2007, 03:35 PM
  2. Class Templates and member functions
    By Mr_roboto in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2006, 10:46 PM
  3. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM