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

  • Home
  • SEARCH
  • 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 8496373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:50:09+00:00 2026-06-10T23:50:09+00:00

I am learning C++, and I’m trying to learn more about using the friend

  • 0

I am learning C++, and I’m trying to learn more about using the friend keyboard.

However, I am having trouble using nested classes in my Header file.

I know that header files should only be used for declarations but I didnt want to include a cpp file with it so I just used a header file to declare and build.

Anways, I have a main.cpp file that I want strictly to be used for creating objects of classes and accessing its functions.

However, I dont know exactly how to create the FriendFunctionTest function in my header file to where I can access it in my main.cpp source file using the header Class object because I’m trying to understand the “friend” keyword.

Here is my header code:

#ifndef FRIENDKEYWORD_H_
#define FRIENDKEYWORD_H_

using namespace std;

class FriendKeyword
{
    public:
        FriendKeyword()
        {//default constructor setting private variable to 0
            friendVar = 0;
        }
    private:
        int friendVar;

    //keyword "friend" will allow function to access private members
    //of  FriendKeyword class
    //Also using & in front of object to "reference" the object, if
    //using the object itself, a copy of the object will be created
    //instead of a "reference" to the object, i.e. the object itself
    friend void FriendFunctionTest(FriendKeyword &friendObj);
};

void FriendFunctionTest(FriendKeyword &friendObj)
{//accessing the private member in the FriendKeyword class
    friendObj.friendVar = 17;

    cout << friendObj.friendVar << endl;
}

#endif /* FRIENDKEYWORD_H_ */

In my main.cpp file, I wanted to do something like this:

FriendKeyword keyObj1;
FriendKeyword keyObj2;
keyObj1.FriendFunctionTest(keyObj2);

But obviously its not going to work since the main.cpp cant find the FriendFunctionTest function in the header file.

How do I fix this issue?

And I apologize again, I’m just trying to learn C++ online.

  • 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-06-10T23:50:11+00:00Added an answer on June 10, 2026 at 11:50 pm

    The friend keyword is only used to specify if a function or other class can have access to the private members of that class. You have no need for class inheritance or nesting because FriendFunctionTest is a global function. Global functions do not require any class prefixes when invoked.

    Source for friend: http://msdn.microsoft.com/en-us/library/465sdshe(v=vs.80).aspx

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

Sidebar

Related Questions

For learning purposes, I'm trying to make a function using Python that takes in
Learning about Explicit Cursors and trying to create my frst one.: SET SERVEROUTPUT ON
Learning more about code contracts, they seem like something I want to use in
Learning Android I'm trying to find contacts using DISPLAY_NAME selector. I need to find
Learning about notifyAll made me question something about notify: in a typical situation we
Learning jquery, so please be kind :) Using PHP, I have a table with
Learning CasperJS Trying to understand why the following is not displaying my results in
Learning how to do a master page in asp.net. Trying to figure out how
Learning java server technologies, trying to clarify some things. There are few technologies that
learning about loops (still a beginner) in VB.net. I have got the below code

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.