找了一个c++,classtemplate的例子,为啥不能编译通过?
kofwr 2006-12-22 09:28:18 // 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;
}
错误输出:
g++ ClassTemplate.cpp -o ClassTemplate.exe
ClassTemplate.cpp:15: syntax error before `::' token
ClassTemplate.cpp:16: ISO C++ forbids declaration of `getmax' with no type
ClassTemplate.cpp: In function `int getmax()':
ClassTemplate.cpp:17: `T' undeclared (first use this function)
ClassTemplate.cpp:17: (Each undeclared identifier is reported only once for
each function it appears in.)
ClassTemplate.cpp:17: parse error before `;' token
ClassTemplate.cpp:18: `retval' undeclared (first use this function)
ClassTemplate.cpp:18: `a' undeclared (first use this function)
ClassTemplate.cpp:18: `b' undeclared (first use this function)
ClassTemplate.cpp: In function `int main()':
ClassTemplate.cpp:23: use of `pair' is ambiguous
ClassTemplate.cpp:6: first declared as `template<class T> class pair' here
C:/MinGW/include/c++/3.2.3/bits/stl_pair.h:69: also declared as `
template<class _T1, class _T2> struct std::pair' here
ClassTemplate.cpp:23: parse error before `>' token
ClassTemplate.cpp:24: `myobject' undeclared (first use this function)