friend class declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;


class Ham{
    public:
        Ham();
    private:
        int thing;
        friend void hamFriend(Ham &sfo);
};


int main(){
    Ham ham;
    hamFriend(ham);

    return 0;
}


Ham::Ham(){
    thing = 0;
}

void Ham::hamFriend(Ham &sfo){
    sfo.thing = 9;
    cout << sfo.thing << endl;
}



I get an error that says 'no function hamFriend declared in Ham class.'

what did I do wrong?

also, hamFriend is in private, right?
Last edited on
Friend functions are not supposed to be public, private nor protected.

1
2
3
4
5
6
7
8
9
10
class Ham{

   friend void hamFriend(Ham &sfo);

    public:
        Ham();
    private:
        int thing;
     
}


Edit: I could be wrong, never used friend, but thats what I picked up after a google search or two.
Last edited on
The hamFriend function is not a member of Ham so you should not put Ham:: when defining the it.
but in this video, https://youtu.be/WCFGNdXSzus?t=2m49s

didn't he put friend function inside private?

how do you distinguish private and public.



by the way, if I not put Ham::, it works fine and I don't know why this happens.
To explain on what @Peter87 said. Its because, HamFriend is not part of the class. Its just a friend of the class. It has acces to all of its members etc. You would only put Ham:: on things that belong to the class.

Edit: I love bucky
Last edited on
Declaring something as a friend means that you allow another class or function to access private members of your class.
So in your Ham class, all you are doing is saying "I want to allow the (global) function void hamFriend(Ham &sfo) to access my private members."
That's it, you are NOT actually declaring a function inside of your class.
That's why it doesn't make sense to define void Ham::hamFriend(Ham &sfo) because Ham doesn't have a function like that.
Friend xyz(...) is not a declaration, you are just giving rights to another function.
I have another question.

hamFriend(Ham &sfo)

&sfo means you need to pass address, right? but why do I put ham object instead?

also,

1
2
3
4
void AddOne(int &y)
{
    y = y + 1;
}


if you have to put address to AddOne, and the address is hexadecimal numbers, why do I not get error? shouldn't y be hexadecimal numbers?
&sfo means you need to pass address, right?

Wrong. It means the argument is a reference to an int.
hamFriend(Ham &sfo)
&sfo means you need to pass address, right? but why do I put ham object instead?

It means that the object is passed by reference. sfo is referring to the object that you passed to the function so any changes you make to sfo will affect the object passed to the function (ham in main).

Don't confuse the & here with the operator that you use to get the address (pointer) of an object. & here is NOT an operator. It's part of the type. The & is often written closer to the type like this Ham& sfo. The meaning is the same but it more clearly shows to the reader of the code that Ham& is the type and sfo is the variable name.
Topic archived. No new replies allowed.