2
#include <iostream>
#include<string>
using namespace std;

class Human
{
private:
    string Name;
    int Age;
    friend class Utility;
public:
    Human(string InputName,int InputAge)
    {
        Name = InputName;
        Age = InputAge;
    }
};
class Utility
{
public:
    void DisplayAge(const Human& Person)
    {
        cout<<Person.Age<<endl;
    }
};
int main()
{
    Human FirstMan("Adam",25);
    cout<<"Accessing private member Age via friend class: ";
    Utility::DisplayAge(FirstMan);
}

I don't understand..when I call the function I do send an object(FistMan)..why my compiler still says that I call it without object?

3
  • Make it static. Then it will work. static void DisplayAge(const Human& Person). In utility class, all functions are static in most cases :) Commented Oct 13, 2017 at 13:16
  • You are not calling the function with an object. The class name is not an object. Create an object first: Utility myutility; then call its functions: myutility.DisplayAge(FirstMan);. Commented Oct 13, 2017 at 13:20
  • Yeah,you are right..thank you :) Commented Oct 13, 2017 at 13:27

3 Answers 3

6

DisplayAge in Utility is not a static function. Therefore you need an instance of Uitility in order to call it.

So, either make the function static, or call it via an anonymous temporary

Utility().DisplayAge(FirstMan);

Better still, make DisplayAge a member function of Human.

Sign up to request clarification or add additional context in comments.

Comments

1

Use the static keyword and then you'll be able to call your function on your class

I edited your code below :

#include <iostream>
#include<string>
using namespace std;

class Human
{
private:
    string Name;
    int Age;
    friend class Utility;
public:
    Human(string InputName,int InputAge)
    {
        Name = InputName;
        Age = InputAge;
    }
};

class Utility
{
friend class Human;
public:
    Utility() = default;
    static void DisplayAge(const Human& Person)
    {
        cout<<Person.Age<<endl;
    }
};

int main(void)
{
    Human FirstMan("Adam",25);
    cout<<"Accessing private member Age via friend class: ";
    Utility::DisplayAge(FirstMan);
}

1 Comment

I wouldn't use my answer though as it is bad practice in general. Use the one from @Bathsheba
0

Dön't use a class whenever you want to define functions. Use a namespace:

namespace Utility
{
    inline void DisplayAge(const Human& Person)
    {
        cout<<Person.Age<<endl;
    }
}

int main()
{
    Human FirstMan("Adam",25);
    cout<<"Accessing private member Age via friend class: ";
    Utility::DisplayAge(FirstMan);
}

2 Comments

Age is private and can't be accessed without making more changes.
But you can't friend a namespace; that's important here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.