Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm Sender: cygwin-owner AT sourceware DOT cygnus DOT com Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com From: Holger Burkarth Reply-To: Holger Burkarth To: cygwin AT sourceware DOT cygnus DOT com Date: Sat, 22 May 1999 08:29:11 +0100 Message-ID: X-Mailer: YAM 1.3.5 [040] - Amiga Mailer by Marcel Beck Organization: proDAD-Software Subject: problem with friend function MIME-Version: 1.0 Content-Type: text/plain /* Hello, I have a question to the c++ gurus. Why do not work the friend function ? A friend function is in the same scope as a global declared function - or not ? The sample shows the problem by use Z<>::Compare() as friend. The define USE_FRIEND enables the friend-test. When define USE_FRIEND=1, the compiler ignores the Z<>::Compare() definition and prints errors. Note: The compiler must create function W<>::Cmp as a real function, for referencing the pointer of the function -> constructor of W<>. When I remove this line, all works fine. What's the reason ? Thanks in advance. */ extern void set( int(*)(...) ); //#define USE_FRIEND 1 template int Compare(const T& a,const T& b) { if(a < b) return -1; if(a > b) return 1; return 0; } template struct Z { T Get() const; #if USE_FRIEND != 0 friend int Compare(const Z& k1,const Z& k2) { return Compare( k1.Get(), k2.Get() ); }; #endif }; #if USE_FRIEND == 0 template int Compare(const Z& k1,const Z& k2) { return Compare( k1.Get(), k2.Get() ); }; #endif template struct W { W() { set( ( int(*)(...) )Cmp ); } static int Cmp(const T* k1,const T* k2) { return Compare(*k1,*k2); } }; void func() { W< Z > w; (void)w; } /* $ g++ main.cpp -D USE_FRIEND=1 main.cpp: In function `int Compare>(const struct Z &, const struct Z &)': main.cpp:65: instantiated from here main.cpp:25: no match for `const Z & < const Z &' main.cpp:26: no match for `const Z & > const Z &' gcc version egcs-2.91.57 19980901 (egcs-1.1 release) */ -- Holger Burkarth Software-Developer burkarth AT prodad DOT de http://www.prodad.de -- Want to unsubscribe from this list? Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com