This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Do friend functions work in templates


I have bullheaded through the available documentation to determine how
egcs implements templates (and learning templates at the same time). 
But now I have reached an impass.  I am trying to declare a function to
be a friend of a template and it does not seem to be working. Please
don't hesitate to tell me if I am overlooking something.

My class template:

template <class ENUM_T, int MIN_T = 0> class EnumTemplate
{
public:
  friend ostream& operator << (ostream& s, const EnumTemplate& Me);
   ^ notice friend function.
  EnumTemplate(const string Labels);
  inline EnumTemplate(ENUM_T Value = 0);
  inline void Set(ENUM_T Value);
  inline void Set(int Value);
  inline ENUM_T Get();
  inline const string& Label() const;
  inline const string& Name() const;
  inline const string& Abbrev() const;
  inline ENUM_T Max();
private:
  static vector<string> Labels_m;
  static string Name_m;
  static string Abbrev_m;
  ENUM_T Value_m;
  bool IsIllegal_m;
  int IllegalValue_m;
};

My friend function template which comes after the class template:

template <class ENUM_T, int MIN_T> 
ostream& operator << (ostream& s, const EnumTemplate<ENUM_T, MIN_T>& Me)
{
  if(Me.IsIllegal_m)
  {
    s<<"IL:"<<Me.IllegalValue_m;
  }
  else
  {
    s<<Me.Label();
  }
  return s;
}

The compiler warning and error:

/opt/egcs/bin/g++ -c -Wall -g -I.  TA_Enums.C
In file included from TA_Enums.H:3,
                 from TA_Enums.C:1:
EnumTemplate.H:13: warning: friend declaration `class ostream & operator
<<(class ostream &, const class EnumTemplate<ENUM_T,MIN_T> &)'
EnumTemplate.H:13: warning:   declares a non-template function
EnumTemplate.H:13: warning:   (if this is not what you intended, make
sure
EnumTemplate.H:13: warning:   the function template has already been
declared,
EnumTemplate.H:13: warning:   and add <> after the function name here)
[-snip-]
/opt/egcs/bin/g++ -o TestA -Wall -g TA_Enums.o  TestA.o 
Undefined			first referenced
 symbol  			    in file
operator<<(ostream &, EnumTemplate<LandUnitTypeType, 0> const &)TestA.o
operator<<(ostream &, EnumTemplate<AirUnitTypeType, 0> const &)TestA.o
operator<<(ostream &, EnumTemplate<SurfUnitTypeType, 0> const &)TestA.o
operator<<(ostream &, EnumTemplate<SubSurfUnitTypeType, 0> const
&)TestA.o

I have tried prototyping the function in front of the class template but
this does not seem to help

The environment:
SunOS ultra2a 5.6 Generic sun4u sparc SUNW,Ultra-2
egcs version 1.1.1

	Thanks in advance
	Michael
-- 
First Rule of Engineering Design:
It is wiser to present an unworkable design rather
than a workable design to speed up the revision process
Phone (voice): (812)854-1101    (fax):   (812)854-3437


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]