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

Overloading << Cannot Access Inherited Private Data

I am trying to overload the operator<< from the class second. The problem is some of the data I am trying to access is private in class first. Why am I unable to access the private data since I am using the friend function? I found that the overload…
drowsell
  • 21
  • 5
-2
votes
1 answer

Multiple function definition for templated function due to inheritance

I cannot compile my program with VS2015 due to a function using a templated parameter and inheritance. The error is this one. I'm trying to achieve the following : class A { //do something }; class B : public A { //do…
Wassim
  • 386
  • 2
  • 15
-2
votes
1 answer

Accessing private struct members of a different class

Hi I'm having some trouble understanding the friend keyword from C++ Let's say i have a class as shown below: class A { private: friend class B; struct Astruct { int funVar = 1; }; Astruct myStruct public: …
mrmagin
  • 61
  • 1
  • 7
-2
votes
2 answers

C++ friend function can't access public function of the class

This is an excerpt of an implementation of a Stack class in C++: Stackdemo.hpp #include using namespace std; template class Stack { private: int top; T *arr; public: Stack(int size) { …
DimK
  • 301
  • 4
  • 16
-2
votes
2 answers

std::ostream : class is inaccesible [C++]

I am getting this error in my implementation: struct bookdatabase::Bookdatabase::Book class "bookdatabase::BookDatabase::Book" is inaccessible None of the following solutions solved my problem: std::basic_ostream is inaccessible in C++ cannot…
James Patmon
  • 11
  • 1
  • 2
-2
votes
1 answer

enable_shared_from_this for friend class

I've got a class with a private ctor only constructible by friend classes using a create method: class B { friend class A; private: B(A* parent) { m_parent = parent; }; A* m_parent; }; class A { public: A* createB() { return new B(…
-2
votes
2 answers

Accessing a {member of vector} of a {vector of a friendly class}

I wanted to create a class (Army) that consists of a vector of another class (Human). When trying to access a member of Human through Army I ran into a Segmentation fault. The following code is reduced to the necessary: #include #include…
Josephus
  • 1
  • 2
-2
votes
1 answer

c++ oop program doesn't give expected result

Consider the following piece of program: class cls { int vi; public: cls(int v=37) { vi=v; } friend int& f(cls); }; int& f(cls c) { return c.vi; } int main() { const cls d(15); f(d)=8; cout<
Vasile Turcu
  • 153
  • 11
-2
votes
3 answers

How can we have friend declaration "friend elaborated-class-name ;" as example in c++?

I am confused in elaborated class name. I would be extremely grateful if described as example. Syntax: friend elaborated-class-name ;
-2
votes
1 answer

Extracting specific value (char & int) from a text file and inserting into multiple variables C++

Below is the file I need to extract data from. auto1 engine: gasoline max_speed: 250 engine_cc: 1980 avg_consumption_urban: 11 avg_speed_urban: 50 avg_consumption: 8 avg_speed: 100 auto2 engine: diesel …
Alexandru Nutu
  • 431
  • 2
  • 5
  • 11
-2
votes
1 answer

How to make structure where container's element can call containers only one private function

I want to make structure where container element can give callbacks to containers, but I dont want to make it be public. How should I make it ? I'm not guaranteed, maybe I need statically give container's private functions adress to element or use…
Vinigas
  • 464
  • 5
  • 18
-2
votes
2 answers

Friend function not allowed to access private member

I thought friend functions had access to all members. Even in this question it worked: C++ friend function can't access private members The answer given in that question seems identical to my code, and his compiled fine while mine just says array_…
user3444650
  • 117
  • 9
-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…
-2
votes
1 answer

C++ unexpected syntax errors

Ok I have updated the code: #ifndef VECTOR_H #define VECTOR_H #include #include #include using namespace std; template < typename T> class MyClass { public: MyClass() : size(10), index(0) { vec = new T[10]; }; …
Mihai
  • 55
  • 6
-2
votes
4 answers

Template factory function with additional args: friendship issue

I'm working on a piece of code that looks like this: template class A { // makeA should become a friend A() {} }; template A makeA(const U & u) { (void) u; return A(); } int main() { …
DarioP
  • 5,377
  • 1
  • 33
  • 52
1 2 3
99
100