Questions tagged [friend-function]

In C++ friend function is a kind of function that is a "friend" of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public.

Wiki

A friend function in a C++ class is defined outside the scope of a class but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows

Example

// friend_functions.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class Point
{
    friend void ChangePrivate( Point & );
public:
    Point( void ) : m_i(0) {}
    void PrintPrivate( void ){cout << m_i << endl; }

private:
    int m_i;
};

void ChangePrivate ( Point &i ) { i.m_i++; }

int main()
{
   Point sPoint;
   sPoint.PrintPrivate();
   ChangePrivate(sPoint);
   sPoint.PrintPrivate();
}

Tag usage

The tag can be used for programming related problems in implementation of friend functions, Theoretical questions such as "How to use friend functions" should be avoided

Read more

368 questions
0
votes
1 answer

Is it a bug of C++ gcc HEAD 10.0.0 20190 relative to friend functions

The following program compiles with using clang HEAD 10.0.0 #include template void f( const T & ); class A { public: A( int x = 0 ) : x( x ) {} friend void ::f( const A & ); private: int x; }; template
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
votes
2 answers

friend not getting the private members

I've a class called Packet That I want to serialize with QDataStream I overloaded operator>> and operator<< and in the over loaded function I called stream << somIntMember Though Its declared as friend its complaining for Private Variables error:…
Neel Basu
  • 12,638
  • 12
  • 82
  • 146
0
votes
1 answer

Type-mismatch in Eclipse while calling friend function with a reference to an object via the "this" pointer

When I try to call a friend function of a certain class by passing a reference to an object of said class via de-referencing the "this" pointer, Eclipse throws a type-mismatch error but the program still compiles and runs just fine. To see if this…
0
votes
1 answer

Forward declared singleton class with a friend function from the class with the forward declaration

I hope this is a coherent question... I have a singleton class definition like: #include A.h class Singleton { public: // If I change this to a return by pointer, it works fine. But reference gives me problems. static Singleton…
kovac
  • 4,945
  • 9
  • 47
  • 90
0
votes
2 answers

How to fix "*private variable* is a private member of '*class name*' error

I'm writing code that uses friend functions but I am not sure why I get the error "is a private member of" in the function "sum" since I declared the function as a friend in the header file. Header File: #include class…
0
votes
1 answer

Incomplete type coming when using friend function

Why it is saying incomplete type, why can't I use friend function like this? #include using namespace std; class test; class test2{ public: void outd(test t) { cout <
0
votes
3 answers

Defining operator<< Inside Class

Consider the following code: class MyClass { template friend MyClass& operator<<(MyClass& MyClassReference, Datatype SomeData); // ... }; template MyClass& operator<<(MyClass& MyClassReference,…
Maxpm
  • 24,113
  • 33
  • 111
  • 170
0
votes
0 answers

Accesssing a private struct defined in class by a friend or a member function

I want to make a public: or friend function to a class return the address of a pointer whose type is defined as a private member of the class. Example:- class LinkedList{ struct Node{ int data; Node* next; }; Node* head = NULL; Node* tail…
0
votes
1 answer

Errors trying to overload template class friend << operator

Trying to code a better version of array type i have run into an issue. For some reason the declaration doesnt work. It throws at me bunch of weird errors. Tried looking up the issue but havent found anything so far. Here is the code: Template…
0
votes
0 answers

How friend functions in template classes are compiled when they are implemented in a separate file?

Thanks in advance for your attention! I had written a simple linear algebra library that includes implementations for vetcor, point, and matrix classes. I had added the prototypes of the friend functions such as << , * , + , - operations in a…
0
votes
0 answers

Private Data member is inaccessible in Friend Function

The private data member is inaccessible. Although i have declared function as friend of class. Can anyone help me. class ONE; class TWO { public: void print(ONE& x); }; class ONE { private: int a, b;…
Shoaib Khan
  • 51
  • 1
  • 10
0
votes
0 answers

selfpointer inside template friend function

How can I get pointer to object in friend function? def.h class A { public: template friend void foo(some_type_t &val); A(); virtual ~A(); private: int x; inline A* GetSelfPtr(){return…
HeyHo
  • 1
  • 2
0
votes
1 answer

Overloaded operator> friend in class template

I am trying to have an overloaded operator> friend function within my templated class. The goal of this overloaded operator is to determine if the left array is larger than the right array, irrespective of the type. I want something along the lines…
Troy Leu
  • 259
  • 1
  • 2
  • 11
0
votes
0 answers

Overloading << in friend function leads to "Undefined symbols for architecture x86_64"

I'm trying to compile the following code: Numero.h #include using namespace std; class Numero { public: explicit Numero(double n) : n(n) {}; Numero operator+(const Numero& n2); Numero operator-(const Numero& n2); Numero…
Victor Ribeiro
  • 577
  • 7
  • 20
0
votes
1 answer

How to define a friend function operator>> inside a local class?

Trying to overload input operator >> inside a local class. I tried to define friend istream &operator >> inside class Data. int readFile(char* file_name,float temperature_data[][31]) { class Data { public: int…