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
-1
votes
2 answers

Program in c++ to use 2 classes and find maximum of 2 numbers

I want to find the maximum of 2 numbers but instead of the simple method, i need to use 2 classes and friend functions. How to implement it? I am using the following code but the code isn't working. #include using namespace std; class…
Shubam Bharti
  • 73
  • 1
  • 2
  • 11
-1
votes
1 answer

error in friend function declaration c++

I am getting a compiling error. ||=== Build: Debug in 24.06.01 (compiler: GNU GCC Compiler) ===| G:\C++\24.06.01\main.cpp|22|error: 'void sum(Sb1, Sb2)' redeclared as different kind of symbol| G:\C++\24.06.01\main.cpp|5|note: previous declaration…
-1
votes
1 answer

overloading operators and friend functions

I am having trouble implementing friend functions with overloading operators. I do not have a solid understanding of what I need to use these functions for. If anyone can give me some guidance on how to proceed. Please and thank you in…
Isis Curiel
  • 160
  • 2
  • 14
-1
votes
1 answer

C++, accessing a friend function data within a class

I've written a program meant to collect the daily expenditure information from a user. I was able to get this part of the program to work. It collects data and then displays the data. To expand on the program, I now want it to store data in a text…
Big Head
  • 49
  • 6
-1
votes
2 answers

Addition operator overload for pointers to nested classes

I am working on a linked list class which contains a private nested node class. I want to advance n nodes forward in my list by using the overloaded addition operator but clang is giving me the error "overloaded 'operator+' must be a unary or binary…
-1
votes
4 answers

C++ error C2027: use of undefined type 'second' (Friend Class)

I am learning friend functions (C++) but I can not understand why this code does not work. I get this error: "error C2027: use of undefined type 'second'". (line number 6) It is just an example of course (useless). I am trying to use a member…
user5507798
  • 57
  • 1
  • 8
-1
votes
2 answers

My ostream and istream friend function can't access private class members

My code: matrix.h #include class Matrix { private: int row; int col; int **array; public: Matrix(); friend std::ostream& operator<<(ostream& output, const Matrix& m); }; matrix.cpp #include "matrix.h" #include…
-1
votes
1 answer

Friend list access issue

Is it possible to access friend list of a Facebook user through my iOS app? I would access without any problem for users who accept the app but I want complete friend list. It seems that in the past it was possible and now not possible anymore. I…
Mick
  • 21
  • 3
-1
votes
1 answer

Redefinition of a class in C++

I'm new to C++ and stuck in a problem with an Error, called "Semantic Issue: Redefinition of 'B' cB.h". I have two classes, A and B, where cA should handle an Object of cB by reference and one friend function of cA, fExample. This is what the code…
-1
votes
1 answer

If you introduce a friend within a class in the global namespace, the friend is injected globally

You can inject a friend declaration into a namespace by declaring it within an enclosed class: namespace Me { class Us { friend void you(); }; } Now the function you( ) is a member of the namespace Me. If you introduce a friend within a class in…
Maddyfire
  • 61
  • 5
-1
votes
1 answer

Automatic type deduction in template function using namespaces and friend declaration doesn't work in Clang

I have some trouble compiling a piece of C++ code using Clang 3.3 on linux. However the same piece of code compiles with gcc 4.8.2 as well as Intel Compiler. So I wanted if my code is actually legal. Usually I trust clang more with such questions…
craffael
  • 373
  • 1
  • 10
-1
votes
1 answer

friend template function in a regular class

I think I need a template friend function in a normal class. This function will be doing some complicated allocation in shared memory and some other poking around in the hardware and OS. But I have excluded all of that to show the problem is related…
wapadomo
  • 57
  • 1
  • 5
-2
votes
1 answer

Getting undefined reference to a properly defined friend function -_-

So... I was happily working on a project and then I was going to implement a friend function, which returns an object of the class after generating a random number of arbitrary size. On compilation, it's showing the error undefined reference... blah…
-2
votes
1 answer

Class doesn't have the member function

I was trying normal friend function. And I got stuck at this point. It is showing that class Complexnos has no member add. #include using namespace std; class Complexnos { private: int real,img; public: void read() { …
Pratham Yadav
  • 31
  • 1
  • 7
-2
votes
1 answer

How do I overload the + operator to add 2 objects of same class (each with 3 numbers) to create 1 object with all 6 numbers?

I am currently doing a programming project where I have declared 2 objects of a class called Statistician. The objects are called s1 and s2. Each object uses a function to read in 3 values of type double into the sequence. Then, for each sequence I…
1 2 3
24
25