This is the mail archive of the gcc-bugs@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]

Friend and member fcn with same name, compiler barfs on friend


// bug02.C -- Friend and member fcn with same name, compiler barfs on friend
/*

A template class has a friend function and a member function of the same name
(different arguments).  The friend returns an object of class type.  The
compiler claims that the class type is invalidly undefined, in the friend
declaration.  But if the friend is *defined* within the class, the error does
not occur.

Version:	gcc v2.95.2
Configure:	--prefix=/usr/local --enable-shared \
		--enable-languages=c++,f77,java
Environment:	i686-pc-linux-gnu 
		(Slackware 7.1, kernel 2.2.16, Intel Pentium 3)
Command line:	g++ bug02.C
Submitted-date:	2000-09-10
To:		gcc-bugs@gcc.gnu.org
Submitter:	James F. Carter <jimc@math.ucla.edu>

James F. Carter        Voice 310 825 2897	FAX 310 206 6673
UCLA-Mathnet;  6115 MSA; 405 Hilgard Ave.; Los Angeles, CA, USA  90095-1555
Internet: jimc@math.ucla.edu (finger for PGP key)
UUCP:...!{ucsd,ames,ncar,gatech,purdue,rutgers,decvax,uunet}!math.ucla.edu!jimc

Output complained about:
bug02a.C: In instantiation of `T<int>':
bug02a.C:59:   instantiated from here
bug02a.C:42: invalid use of undefined type `class T<int>'
bug02a.C:48: forward declaration of `class T<int>'
bug02a.C: In function `class T<int> makeaT<int>(const T<int> &)':
bug02a.C:60:   instantiated from here
bug02a.C:28: `int T<int>::content' is private
bug02a.C:56: within this context

*/

#include <iostream.h>

template <class B> class T {
  private:
    B content;
  public:
    T() { }
    T(B c) : content(c) { }
    T(const T& t) : content(t.content) { }
    
	//The friend functions are OK when:
	//  Code at	Class usage		<> present
	//  here	just T			no
	//  here	T<B>			no
	//  separate	T here, T<B> there	yes
	//  separate	T<B> here, T<B> there	yes
	// Despite various warnings you don't have to predeclare the functions.
    friend ostream& operator << <> (ostream& f, T<B>& t);
    friend T makeaT <> (const T& c);		//<== Invalid use of undef type
	// If there is a member function with the same name (different 
	// arguments, whatever return type):
	//	It works ONLY IF the definition is with the friend declaration.
	//	For a declaration only, T<B> appears to be undefined.
    T makeaT();
};						//<== Forward decl of T<>

template <class B>
/*friend*/ inline ostream& operator << (ostream& f, T<B>& t)
	{ return f << t.content; }

template <class B>
/*friend*/ inline T<B> makeaT (const T<B>& c)
	{ return T<B>(c.content); }		//<== Private data member

int main() {
    T<int> s(0);
    T<int> t = makeaT(s);
    cout << "t = " << t << '\n';
    return 0;
}



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