0

I've tried to figure this out for an hour now and I've gotten nowhere.

I have a class with a friend function and private members but I am getting a compiler error telling me that I cannot access a private member using that friend function.

line 36 error: 'family* family::famPtr' is private

The friend prototype is as follows within the class body

friend void output(family *famPtr);

The private members are as such:

private:
    string husband;
    string wife;
    string son;
    string daughter1;
    string daughter2;
    family *famPtr;

And this is the function call itself within the main function for a family object Simpson.

output(Simpson.famPtr);

I'm not sure where I'm messing up here, it seems relatively simple and my textbook is getting me nowhere and none of the things I've found on here have led me in the right direction.

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
  • Is your friend function in the class `Simpson` or in `family`? Also try posting more code or the line on which the error occurred. It is very hard to debug your code without your code –  Aug 24 '15 at 17:45
  • 2
    This question is useless not having the context (class) of the friend function –  Aug 24 '15 at 17:45
  • 5
    `friend` means that `output` can access stuff, not `main`. – chris Aug 24 '15 at 17:46
  • 1
    @chris You often answer the questions as a comment ... why not answer them with an answer? – Fantastic Mr Fox Aug 24 '15 at 17:50
  • 1
    @Ben, I don't know, it feels too short. There's no explanation behind it. – chris Aug 24 '15 at 17:52
  • 1
    @chris seems like the perfect length! Succinct and all relevant. I often see you comment the answer followed by someone else writing the same thing. Anyway, just curious. – Fantastic Mr Fox Aug 24 '15 at 17:54
  • @Ben [**Nope!**](http://stackoverflow.com/help/mcve) – πάντα ῥεῖ Aug 24 '15 at 17:59
  • 1
    @Ben Some people just refuse to get up-votes for answering trivial questions (also do not up-vote these) –  Aug 24 '15 at 18:00
  • @πάνταῥεῖ I was referring to Chris's comment, and if it was an answer i feel that it would be perfect. – Fantastic Mr Fox Aug 24 '15 at 18:04
  • @DieterLücking I guess so ... – Fantastic Mr Fox Aug 24 '15 at 18:04
  • @Ben Well, _@chis_ answer might hit the point, but first OP is expected to clarity their question by means of that link I've given. I'm acting in a similar way often, I just avoid to answer unclear questions but in comments, and rather downvote and close vote them. These kind of questions are usually not helpful for future research. – πάντα ῥεῖ Aug 24 '15 at 18:12
  • You need to show the `family` class, the definition of `output()` and the code where you're calling `output()`. It's possible to guess what your issue is ( you're trying to call `output()` from a function that doesn't have visibility to `family::famPtr`) but without seeing all of the code, we can't be sure. – Rob K Aug 24 '15 at 18:20

3 Answers3

3

You may not call the function the following way

output(Simpson.famPtr);

because relative to the scope where the function is called data member Simpson.famPtr is private.

It is within the function where you could use expression Simpson.famPtr.

That is it is the function itself that is the friend. It is not the scope where the function is called is the class friend.

If the class would contain a public accessor like for example

family * get_family() const;

then you can call the function like

output(Simpson.get_family() );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

There seems to be a design flaw in your program.

Making a function that takes a family* as input a friend of the class Simpson does not make sense at all.

The function does not deal with Simpson, It deals with family. How will making that function a friend of Simpson help?

It's difficult for me to suggest a solution since it's not clear to me why you wanted the friend-ship in the first place.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @R Sahu It is entirely a wrong conclusion. The function for example can create an object of the class or call other functions that return objects of the class and this function heeds to access their data members. – Vlad from Moscow Aug 24 '15 at 18:03
  • @VladfromMoscow, that's entirely possible. It doesn't make sense at first glance. Hence the last sentence in my answer. – R Sahu Aug 24 '15 at 18:05
0

Making a function a friend allows it to access private members. It does not allow access in all calls to the function

output(Simpson.famPtr);

Here the access to famPtr is not made by output

If you change output to

output(family & outer)
{
   old_output(outer.famPtr);
}

Then the access to private member famPtr is contained within the friend function

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80