class templates in main()

This is a discussion on class templates in main() within the C++ Programming forums, part of the General Programming Boards category; So I understand how templates work, but I don't understand how to use them in a main (or at least ...

  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,328
    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, 10: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, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21