473,695 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

friend template function

I am not clear about friend functions of a template class.
GCC (3.3.2) wants me to add <> after the friend function names in the
class declaration and VisualC++ doesn't like that.

template <class T>
class test{
test(void);
~test(void);
friend bool operator== <> (const test<T> &p1, const test<T> &p2);
}

If I don't add <> I get from GCC the following warning:
"friend declaration `bool operator==(cons t test<T>&,
const test<T>&)' declares a non-template function
(if this is not what you intended, make sure the function
template has already been declared and add <> after the function name
here) -Wno-non-template-friend disables this warning"

Can someone explain me what I am missing?

Thanks,
Christophe
Jul 19 '05 #1
2 12529
Christophe Barbe wrote:
I am not clear about friend functions of a template class.
GCC (3.3.2) wants me to add <> after the friend function names in the
class declaration and VisualC++ doesn't like that.

template <class T>
class test{
test(void);
~test(void);
friend bool operator== <> (const test<T> &p1, const test<T> &p2);
}

If I don't add <> I get from GCC the following warning:
"friend declaration `bool operator==(cons t test<T>&,
const test<T>&)' declares a non-template function
(if this is not what you intended, make sure the function
template has already been declared and add <> after the function name
here) -Wno-non-template-friend disables this warning"

Can someone explain me what I am missing?


You don't seem to have a coherent definition.
template <class T>
class test{
test(void);
~test(void);
friend bool operator== (const test<T> &p1, const test<T> &p2);
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^
This is a declaration of operator== that takes 2 const test<T> & parameters.
};

However a friend declaration is referring to somthing outside the class
but you have test<T> as a parameter which has no meaning outside the
class since T is a template parameter and is only known when test<T> is
instantiated.

What you might be trying to say is:

template <class T>
class test{
test(void);
~test(void);
template <class T2> friend bool operator== (const test<T2> &p1, const
test<T2> &p2);
};

In this case operator== is a template which takes it's own T2 parameter.


Jul 19 '05 #2
On Wed, 15 Oct 2003 15:48:19 +0000, Gianni Mariani wrote:
template <class T>
class test{
test(void);
~test(void);
friend bool operator== (const test<T> &p1, const test<T> &p2);
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^
This is a declaration of operator== that takes 2 const test<T> & parameters.
};

However a friend declaration is referring to somthing outside the class
but you have test<T> as a parameter which has no meaning outside the
class since T is a template parameter and is only known when test<T> is
instantiated.
Do you mean that a friend function of a template class should not be
defined outside of the template class declaration?
What you might be trying to say is:

template <class T>
class test{
test(void);
~test(void);
template <class T2> friend bool operator== (const test<T2> &p1, const
test<T2> &p2);
};

In this case operator== is a template which takes it's own T2 parameter.


I don't have a second T2 parameter.

I have the following after the template class declaration:

template <class T>
inline bool operator== (const test<T> &p1, const test<T> &p2) {
if (p1.X == p2.X && p1.Y == p.Y) return true;
return false;
}

is the declaration:
friend bool operator== (const test<T> &p1, const test<T> &p2);
not adapted?
GCC is happy with:
friend bool operator== <> (const test<T> &p1, const test<T> &p2);

Thanks,
Christophe
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10154
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available online at http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.15 Below are the two files that I compile with g++ foo.cpp -o foo or
1
4443
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external 'aFunc(A<char> const&)' ) in the following sample program. Tried it on GCC and Borland's compiler. Can somebody please show me the correct way to fix this? Thanks, Dmitry
5
2714
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
3
2087
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ============================================================== Question 1: ============================================================== Compile the following codes, and run it.
0
1693
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > > ============================================================== > Your code refuses to compile. In function 'foo<a>' 't' is a pointer to > const. Friendship has nothing to do with it. When I remove the attempt > to change 't->c' in foo<a>, it compiles and runs and displays > > T<a, 3>
5
2633
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
4
774
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
9
2060
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
21
4733
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of behavior I'm searching for so I was hoping there was a way to do the same thing in gcc. As you know...
0
8555
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8824
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5831
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4336
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4571
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.