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
0 answers

friend function is not assigning values to class's array

i'm new to C++ programming and i created this program for vector matrix multiplication,everything is working fine , except results are not stored in my class's array(i'm using friend function to do multiplication and assign result) . here is my…
user14766186
0
votes
1 answer

Calling private member of inherited class for unittest

I'm trying to write a unittest but I'm running into some problems. I've got a class which has an int to keep track of the current state. All classes that are inherited of this class can change the state by calling the protectedFunction. class…
Red-ER
  • 173
  • 2
  • 11
0
votes
1 answer

Track location of friends

I would like to make it possible for the user to track their friends through one of my applications. The application is for a music festival. I have been thinking of ways to do this: Let the user set a nickname for the device (associated with the…
simonbs
  • 7,932
  • 13
  • 69
  • 115
0
votes
0 answers

Create an array using class members

In my .hpp file I have two classes definitions: class c1 { protected: int size; .... public: read_data() { } } class c2 { friend class c1; public: void method() { } ... } Then, in the .cpp…
diego
  • 1
  • 1
0
votes
1 answer

How to declare a global-scope function a friend to a namespaced class?

The following code defines class Foo in namespace Namespace. // main.cpp #include namespace Namespace { class Foo { private: void doSomething() {}; friend void func( union sigval ); }; } static Namespace::Foo foo; void func( union…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
0
votes
0 answers

Friending class while there's an object of friending class inside friended class

the title might seem a little bit confusing but I'll try to explain that. I've got a class CGameMode: #pragma once #include #include #include "../Hero/Hero.h" #include "../Weapon/Weapon.h" #include "../Armor/Armor.h" class…
Vatnax
  • 11
  • 1
0
votes
0 answers

Classes With Friend Functions of Each Other

Is it possible to create a class that has a friend function of a forward declared class? If I wanted to create two classes that have friend functions of each other, how would I do that? This attempt fails with the message invalid use of incomplete…
FuzzyCat444
  • 315
  • 2
  • 7
0
votes
3 answers

How to access friend function from another class?

When I compile the following code, #include using namespace std; class class1;//forward declaration class class1{ int var; public: void setter(int); friend void getter(class1 o1); }; class class2{ public: void getter(class1…
Meganathan
  • 25
  • 6
0
votes
3 answers

Sharing friends between classes via include file

I have some old c++ code where friend classes are all declared outside of a class in a bare include file. The include file is then shared between different classes Such as follows IncludeFriends.h #pragma once friend class Friend1;// Error…
N Strahl
  • 75
  • 8
0
votes
1 answer

Method from a class declared as friend of another one raises error

I have the following two classes Player and FriendOfPlayer: Player.hpp #ifndef PLAYER #define PLAYER #include"FriendOfPlayer.hpp" #include using namespace std; //class FriendOfPlayer; class Player{ public: // getters…
floflo29
  • 2,261
  • 2
  • 22
  • 45
0
votes
2 answers

Not able to understand why friend function is not working

#include using namespace std; class A; void test(A &a); class A { private: int x; int y; friend void test(); }; void test(A &a) { a.x = 1; a.y = 2; cout << a.x << " " << a.y << endl; } int main() { …
0
votes
1 answer

declaring a friend function in the global namespace that returns a template class

i have a function declared in a namespace, that when it is defined as a friend, throws a compiler error. #include #include namespace Example { std::vector GetNames(); } class MyClass { public: …
CommanderBubble
  • 333
  • 2
  • 9
0
votes
2 answers

Stream Insertion Operator << Overloading Undefined Symbols for architecture x86_64 Error

I'm currently learning how to overload the stream insertion operator for my class. Found below is the code I have: #include #include using std::string; using std::endl; using std::cout; template class A { …
Chronollo
  • 322
  • 1
  • 9
0
votes
3 answers

friend member function can't access private member data

I try to access a private member data of class X, with a friend function, which itself is a member function of class A. Here's the code: class X { int foo; public: friend void A::func(X x1); }; class A { public: void func(X x1) { x1.foo…
0
votes
3 answers

Global namespace friend class cannot access private member of named namespace class

In a named namespace class, I declare a class (which is in the global namespace) as friend. However, the latter class cannot access the private member of the former class. Why is this? Is there any way around it? Bob.h namespace ABC { class Bob…
didjek
  • 393
  • 5
  • 16