friend template function

C

Christophe Barbe

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==(const 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
 
G

Gianni Mariani

Christophe said:
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==(const 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.
 
C

Christophe Barbe

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top