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
0
votes
1 answer

C++ friends in template classes, separation of interface and implementation

The following code works fine: Class.h: #ifndef ClassLoaded #define ClassLoaded #include template class Class{ public: template friend std::ostream& operator<<(std::ostream& Stream, const Class&…
Mario
  • 123
  • 5
0
votes
1 answer

Using attribute in class from friend and heritage c++

I think I really should explain myself about my question :p So, I have a class c (let's start from the end), in this class c I want to use an attribute which is defined in class A. But my class c does not inherit from A, it is a friend of class b…
Remi Hirtz
  • 134
  • 3
  • 16
0
votes
1 answer

Friend function can't access private members

I'm trying to get a friend function of class1 and ships to access the private members of both, but it says that those members are inaccessible. The code is below, the problem is in ships.cpp. I tried to reproduce this problem in an even more simple…
shinzou
  • 5,850
  • 10
  • 60
  • 124
0
votes
1 answer

Function declared outside class scope but not friend. How does this work?

I have the following class: class PhoneCall{ private: string number; public: /*some code here */ }; Now, I have declared a function (not friend to PhoneCall) which does some specific operation and returns a PhoneCall object PhoneCall…
Abrar
  • 6,874
  • 9
  • 28
  • 41
0
votes
0 answers

Friend overloaded insertion accessing data members of another class

I have an assignment that I am stuck on and I have researched all day and come up empty handed: I am working with two classes: ClassA must have a unique overloaded assignment and overloaded insertion. ClassB must have a unique overloaded…
DATcat
  • 1
  • 1
0
votes
3 answers

C++ Friend Functions in separate header and .cpp files

//A.h class A{ friend bool compareEntry_sumPct_nonMega(Entry arg1, Entry arg2); } //A.cpp #include "A.h" bool A::compareEntry_sumPct_nonMega(Entry arg1, Entry arg2) { bool b = arg1.sumOfNonMegaEntryPct(numbers) <…
Tommy Saechao
  • 1,099
  • 4
  • 17
  • 28
0
votes
3 answers

Friend operator << without overloading

I have a class complex and I would like to operator << could print its private variables. class complex { double re, im; public: friend ostream operator <<(ostream &out); // What's wrong? }; Is it possible?
0
votes
1 answer

Class member functions as friends in other classes C++

I have to classes that represent random generators. r_6.h #include #pragma once class R_G_6 { public: R_G_6() {}; float getNextRand(); void countFrequency(); void printFrequency(vector); …
niar_q
  • 153
  • 9
0
votes
1 answer

C++ Friend Function not accessing private members

So I am trying to make a chess game ( bit of an ultimate challenge for me ), and I'm a stump for this part .. So I made a piece object, and the idea is that in the main game code, I have an array of pieces and I pass the address of the array to the…
user3502489
  • 361
  • 1
  • 4
  • 11
0
votes
3 answers

C++ cannot convert ‘sender’ to ‘void*’ for argument ‘1’ to ‘void* sending(void*)’

I have startSending procedure and a friend function (sending) inside sender class. I want to call the friend function from a new thread, so I create a new thread inside startSending procedure. class sender{ public: void startSending(); …
ZZZ
  • 1,415
  • 5
  • 16
  • 29
0
votes
2 answers

Friend function defining an ostream operator

I would like to define an ostream operator to let me easily output variables of type alglib::complex. To provide a working example without including the alglib library I'll instead overload the output of complex below (this clarification…
jorgen
  • 3,425
  • 4
  • 31
  • 53
0
votes
1 answer

lookup template function with friend keyword

everyone I am a beginner of c++. Now I try to understand how compiler lookup a function with friend keyword. The followings are the code with warning and error messages. I have two problems in the code. One is warning and another is error. At…
mora
  • 2,217
  • 4
  • 22
  • 32
0
votes
1 answer

Should states in an FSM be friends with the context type?

I've built a class-based Push-down Automaton Finite State Machine. The context class (the class whose internal state is being modified) has some methods that only the states should access (incrementing/decrementing some iterators, pushing/popping…
Casey
  • 10,297
  • 11
  • 59
  • 88
0
votes
0 answers

Undefined reference with friendship and namespace using a static int

I have a class in which I declare a friendship to another class outside the namespace. Now that class has a static integer I want to access. Here is the code: class A { public: void init(); }; namespace C { …
Gerhard Stein
  • 1,543
  • 13
  • 25
0
votes
2 answers

c++ how to properly declare friend class method of another class

consider the following example. class A { int member; }; class B { A& a_ref; void manipulate() { a_ref.member++; } }; Now, obviously, B::manipulate can not access a_ref. I would like to allow (only) class B to get…
Zereges
  • 5,139
  • 1
  • 25
  • 49