friend-function

Template friend function of a template class

I was struggling with the issue described in this question (declaring a template function as a friend of a template class), and I believe the 2nd answer is what I want to do (forward declare the template function, then name a specialization as a friend). I have a question about whether a slightly different solution is actually correct o...

How can friend function be declared for only one particular function and class?

What's wrong with my code? I tried to compile the code below in the GNU G++ environment and I get these errors: friend2.cpp:30: error: invalid use of incomplete type ‘struct two’ friend2.cpp:5: error: forward declaration of ‘struct two’ friend2.cpp: In member function ‘int two::accessboth(one)’: friend2.cpp:24: error: ‘int one::data1’...

Error in linking to friend functions

I have a class 'Vector3' which is compiled successfully. It contains both non-friend and friend functions, for example, to overload * and << operators when Vector3 is the second operand. The problem is I can't link to any of the friend functions, be it operator overloaded or not. So I can confirm that the error is not specific to operato...

friend function in template definition

My question ist related a bit to this one. I want to overload the operator << for some class and I found two different notations that both work: template <class T> class A{ T t; public: A(T init) : t(init){} friend ostream& operator<< <> (ostream &os, const A<T> &a); //need forward declaration //template <class U> friend ostrea...

Where would you use a friend function vs a static function?

Where would you use a friend function vs a static function? ...

How to split the definition of template friend funtion within template class?

The following example compiles fine but I can't figure out how to separate declaration and definition of operator<<() is this particular case. Every time I try to split the definition friend is causing trouble and gcc complains the operator<<() definition must take exactly one argument. #include <iostream> template <typename T> class T...

Declaring "friend" functions fails under Visual Studio 2008

I am trying to declare a global function as a "friend" of a class: namespace first { namespace second { namespace first { class Second { template <typename T> friend T ::first::FirstMethod(); }; } } } When I compile this code under Visual C++ 2008 ...

ADL and friend injection

Consider this code: template <int N> struct X { friend void f(X *) {} }; int main() { f((X<0> *)0); // Error? } compilers seem to heavily disagree. (MSVC08/10 says no, GCC<4.5 says yes, but 4.5 says no, sun 5.1 says yes, intel 11.1 says yes too but comeau says no (both are EDG)). According to "C++ Templates - The complete guide": ...

Friend functions not recognized

I have the following class with a couple friend functions: class Teleport { public: Teleport(); ~Teleport(); void display(); Location teleportFrom(int direction); friend bool overlap(Wall * wall, Teleport * teleport); friend bool overlap(Location location); friend bool overlap(Wall * wall); friend bool o...

calling a function from a friend function defined in a header in c++

Hi all, I have redefined the >> operator as a friend function in a template class in the header. In it, i need to call another function called inputHelper that I have also defined in the header. (input helper is recursive) the header file is as follows: template< typename NODETYPE > class Forest { /* (other friends) */ friend ...