Thread: class templates in main()

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    class templates in main()

    So I understand how templates work, but I don't understand how to use them in a main (or at least the syntax I need to use them).

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    template<class TYPE>
    class pair
    {
    public:
       pair(TYPE x): c1(x),c2(x + 1) {}
       void increment() {c1++; c2++;}
       void print() const {cout << c1 << "\t" << c2;}
    private:
       TYPE c1, c2;
    };
    
    int main()
    {
        int x = 5;
        
        pair<int>pair(x);
        x.pair<int>increment();
        x.pair<int>print();
        
        system("PAUSE");
    }
    All I want to do is declare it, increment it, and print it.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    pair<int> my_pair(x);
    my_pair.increment();
    my_pair.print();

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Notice how the type is pair<int> and when creating and using objects of that type you just use pair<int> wherever you would use a normal class name for a non-templated type that you create.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Templates Again
    By creativeinspira in forum C++ Programming
    Replies: 9
    Last Post: 07-01-2007, 05:13 AM
  2. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  3. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  4. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM