Hi,
I am going through some tutorials on templates and there is one on cplusplus.com that I have been using. It is explaining about class templates and here is the code for one of the examples they have made:
Code:
// class templates
#include <iostream>
using namespace std;
template <class T>
class pair {
T a, b;
public:
pair (T first, T second)
{a=first; b=second;}
T getmax ();
};
template <class T>
T pair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}
int main () {
pair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}
This code looks fine to me and I understand it but when coming to compile this using g++ I get the following errors:
main.cpp:15: error: expected init-declarator before '<' token
main.cpp:15: error: expected `;' before '<' token
main.cpp: In function `int main()':
main.cpp:23: error: `pair' undeclared (first use this function)
main.cpp:23: error: (Each undeclared identifier is reported only once
unction it appears in.)
main.cpp:23: error: expected primary-expression before "int"
main.cpp:23: error: expected `;' before "int"
main.cpp:24: error: `myobject' undeclared (first use this function)
When I remove the "using namespace std;" statement and use the scope resolution operator to access the cout function of the std namespace in main(); "std::cout << myobject.getmax();" instead of "cout << myobject.getmax();" the code compiles fine.
Am I missing something? Can anybody tell me why this is happening?
Thanks