Template class

I have an assignment where I need to pass three different types to a template class

when I try to compile, "pair" is undeclared in int main(). and that also means i can't set pair to t so i can call my functions

help!!!

#include<iostream>
using namespace std;

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()
{
pair t;

pair<int> x(2);
t.print();
t.increment();
t.print();

pair<char> x('A');
t.print();
t.increment();
t.print();

pair<double> x(3.5);
t.print();
t.increment();
t.print();

system("pause");
return 0;

}
Rename your class, as there is another class named pair in the standard libraries.[1]

Also when declaring a template class, you always have to give it's template parameters.

Also you're declaring variables with the same name.

Don't use system(). Use cin.ignore() instead.
(or even better : std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); ), [2]

[1] http://cplusplus.com/reference/std/utility/pair/

[2] http://cplusplus.com/forum/beginner/1988/
Change your class name and constructor to a capital P and in int main()

Pair<int> x(2);
x.print();
x.increment();
x.print();

Pair<char> x2('A');
x2.print();
x2.increment();
x2.print();

Pair<double> x3(3.5);
x3.print();
x3.increment();
x3.print();


Hope this helps. Btw can redeclare x as compiler will complain about a "redefinition" hence the x2 etc.
Also I dont think you can declare a template class like you would a normal class hence the "pair t" not working but im not 100% sure about this. The above code does work though.
Thank you all, that was just the information I needed. I will have to remember the bit about the standard library. I had no idea that there was another pair class.
Topic archived. No new replies allowed.