I am trying to learn about templates, I want my class pair to be able to hold two objects of any type. I just want to provide an accessor function for obj1 now. But I get the following error when I try to complile:
error: expected initializer before '<' token
T1 pair<T1,T2>::getObj1()
My code is:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
template <class T1, class T2>
class pair
{
public:
pair(const T1& t1, const T2& t2) : obj1(t1), obj2(t2){};
T1 getObj1();
private:
T1 obj1;
T2 obj2;
};
template <class T1, class T2>
T1 pair<T1,T2>::getObj1()
{
return obj1;
}
int main()
{
return 0;
}
mypair
instead.using namespace std
. The question would benefit from an answer that actually addresses what the error means in general, not just pointing out that a totally different question (there is no similarity of the titles) has a solution that fits to this particular example.