Nov 6, 2011 at 11:52pm UTC
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;
}
Nov 7, 2011 at 12:08am UTC
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.
Nov 7, 2011 at 12:10am UTC
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.
Nov 7, 2011 at 3:18am UTC
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.