Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:35:15+00:00 2026-05-27T11:35:15+00:00

I have the following code: Master.h #ifndef MASTER_H #define MASTER_H class Master { friend

  • 0

I have the following code:

Master.h

#ifndef MASTER_H
#define MASTER_H

class Master
{
   friend class Friend;

   public:
      Master(void);
      ~Master(void);
      void CallFriendFunction(int largeData);

   private:
      int    largeData;

      //Want this class to share largeData;
      Friend testFriend;
};
#endif // MASTER_H

Master.cpp

#include "Master.h"

Master::Master(void)
{
   //Inentionally left blank
}

Master::~Master(void)
{
   //Intentionally left blank
}

void Master::CallFriendFunction(int largeData)
{
   this->largeData = largeData;
   this->testFriend.Test(this);
}

Friend.h

#ifndef FRIEND_H
#define FRIEND_H

#include "Master.h"

class Friend
{
   public:
      Friend(void);
      ~Friend(void);

      void Test(Master* masterPtr);
};

#endif // FRIEND_H

Friend.cpp

#include "Friend.h"
#include <iostream>

Friend::Friend(void)
{
   //Intentionally left blank
}

Friend::~Friend(void)
{
   //Intentionally left blank
}

void Friend::Test(Master* masterPtr)
{
   std::cout << masterPtr->largeData << std::endl;
}

I want class Friend to be able to share Master’s private members. However, I can’t get this code to compile. I’ve tried Forward Declaration, and #includes, but I start getting into circular dependencies. When Friend class is not a member of Master class, the code compiles?

Is it possible for Friend class be a member of Master and be friends?
How else can Friend class have access to Masters private members?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:35:16+00:00Added an answer on May 27, 2026 at 11:35 am

    You need the following includes and forward declarations:

    In Master.h:

    #include "Friend.h"
    

    In Friend.h:

    class Master;
    

    In Friend.cpp:

    #include "Master.h"
    

    Putting the forward declaration in Friend.h prevents circular dependency. A forward declaration is enough there because you only declare a Master* parameter, without using its members yet.

    You do need to include Friend.h from Master.h because you are declaring a Friend member, and this requires a complete type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a master page with the following code: public partial class MasterPage :
I have the following class... public class MyCustomLogger { public static int DEFAULT_INFO =
I have the following code: private Models.mediamanagerEntities dataModel = new Models.mediamanagerEntities(); public ActionResult Index(FormCollection
I have the following code in my Site.Master page of an almost empty ASP.NET
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following code class User attr_accessor :name end u = User.new u.name =
Consider the following code. public interface IFoo { } public class Bar { public
In the master page I have the following code: <!DOCTYPE html> <html> <head> <meta
I have the following code in my site.master for a menu: <ul id=menu> <li><%=
I have the following code in my HomeController: public ActionResult Create() { return View();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.