1
Reply

How to define a member function outside the class template

Ken H

Ken H

Nov 22 2012 8:11 AM
2.1k
hi,
  I'm try to be defined outside of the class template member function, but it is an error,

code,see below:

#include <iostream>
using namespace std;

template<class NumType>
class Compare
{
 public:
  Compare(NumType a,NumType b)
  {
   x=a;
   y=b;
  }
  NumType max();
  /*{
   return(x>y?x:y);
  }*/
  NumType min();
  /*{
   return(x<y?x:y);
  }*/
 private:
  NumType x,y;
};

template<class NumType>
NumType Compare<NumType>::max()
{
   return(x>y?x:y);
}
NumType Compare<NumType>::min()
{
  return(x<y?x:y);
}
int main()
{
 int i=19,j=27;
 float x=6.5,y=2.9;
 Compare<int> aObject(i,j);
 Compare<float> bObject(x,y);
 cout<<"type of data for int,the max is "<<aObject.max()<<endl;
 cout<<"type of data for int,the min is "<<aObject.min()<<endl;
 cout<<"-----------------------------------"<<endl;
 cout<<"type of data for float,the max is "<<bObject.max()<<endl;
 cout<<"type of data for float,the min is "<<bObject.min()<<endl;
 return 0;

}

thanks.


Answers (1)