Questions tagged [friend]

In object-oriented programming, friend refers to a method or class that has access to some non-public aspects of a particular class.

In object-oriented programming, friend refers to a method or class that has access to some non-public aspects of a particular class.

Different programming languages use the friend keyword differently. Make sure it is clear which language you are asking about.

1518 questions
6
votes
2 answers

friend and template in C++

I have a big problem in my C++ Code example. There is something wrong with 'friend' and the 'template'. Error Messages: Matrix.h:26:79: warning: friend declaration 'std::ostream& matrixClass::operator<<(std::ostream&, const…
6
votes
1 answer

Overloading operator<< - must be a binary operator

What's the error here? I reviewed prior Q&A, but all those coders appear to have made other errors when overloading <<. When I try it, QT Creator gives this error: overloaded 'operator<<' must be a binary operator (has 3 parameters), referring to…
NAR
  • 65
  • 1
  • 1
  • 4
6
votes
3 answers

Friendship in nested classes C++

I am trying to understand concept of friendship in nested classes but I am not getting the concept properly. I have written a sample program to understand it but the program is not working #include using namespace std; class…
Mayank Jain
  • 2,504
  • 9
  • 33
  • 52
6
votes
2 answers

Incomplete type in friend function

This is a mcve of my code: (if it matters, Options_proxy and Options have constexpr ctors). I am aware it's still far from simple, but couldn't simplify it more while still exhibiting the error: template struct Options_proxy : Impl { …
bolov
  • 72,283
  • 15
  • 145
  • 224
6
votes
2 answers

Friends confusion

$11.4/5 - "[...]A friend function defined in a class is in the (lexical) scope of the class in which it is defined[...]" What does this statement mean? struct A{ typedef int MYINT; void f2(){f();} // Error, 'f' is undefined …
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
6
votes
3 answers

Friend template function declared inside template class causing undefined symbol link error

I have been banging my head against this for a couple of days, looking it up and also looking for similar code in open source projects: can't really find what I'm doing incorrectly. Essentially, given the code below (distilled to its…
Marco Massenzio
  • 2,822
  • 1
  • 25
  • 37
6
votes
1 answer

Calling friend function defined in struct requires forward declaration?

While reading Karlsson's Beyond the C++ Standard the author defined the friend function intrusive_ptr_add_ref in the body of class reference_counted (see pg 36). That function is called automatically using Argument Dependent Lookup at the proper…
themis
  • 1,047
  • 11
  • 16
6
votes
4 answers

Unused friend class in C++

Is there a way to detect (for instance with compiler warning) if classes are declared friend but do not access private members, ie. when friendship is useless?
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
6
votes
4 answers

template friend functions of template class

I have the following template class and template function which intends to access the class' private data member: #include template class MyVar { int x; }; template void printVar(const MyVar& var) { …
timrau
  • 22,578
  • 4
  • 51
  • 64
6
votes
1 answer

Friend functions and static data members

How is unqualified name lookup being performed for names using within the friend's function body? Let's consider the following code: #include void foo(); class A { friend void foo(){ std::cout << a << std::endl; } static int…
user2953119
6
votes
4 answers

How to befriend a templated class's constructor?

Why does class A; template class B { private: A* a; public: B(); }; class A : public B { private: friend B::B(); int x; }; template B::B() { a = new A; a->x = 5; } int…
Kyle
  • 4,487
  • 3
  • 29
  • 45
6
votes
1 answer

Set a project default for VB.NET projects so that the default Modifiers property for controls is Private

Is it possible to set a project default for VB.NET winforms projects so that the default Modifier for controls added to winforms is Private (not Friend)? I know there's a "modifiers" property in the properties window so I can set it for each…
Darren
  • 9,014
  • 2
  • 39
  • 50
6
votes
1 answer

operator << friend function and templates

This is my code: mov.h #include template< class T> class Movie { public: Movie(T in) { a = in; } friend std::ostream& operator<<(std::ostream& os, const Movie& movie); private: T a; }; template
user11001
  • 372
  • 3
  • 14
6
votes
1 answer

When is "already a friend" warning useful?

I have code which uses a preprocessor-heavy framework to generate some utility classes. Apparently, some of the macros result in the same friend declaration being included twice in a class, something like this: class Friendly { // ::: friend…
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
6
votes
2 answers

What does `template friend class Foo` mean?

I'm exploring the boost::iterator_facade and came across this bit of code: friend class boost::iterator_core_access; template friend class Iterator; What does the second line mean? I'm familiar with friend classes, but I don't think…
vmrob
  • 2,966
  • 29
  • 40