Reputation: 87
#include<iostream>
using namespace std;
class complex{
private:
int x;
public:
friend void fun(void);
};
void fun(void)
{
cout<<"outside "<<endl;
}
int main()
{
complex c1;
c1.fun();
return 0;
}
why I'm getting the following error which says that 'class complex' has no member named 'fun' in VS code
36_friend_function.cpp: In function 'int main()':
36_friend_function.cpp:19:8: error: 'class complex' has no member named 'fun'
c1.fun();
^~~
Upvotes: 0
Views: 87
Reputation: 22104
The function fun
is a friend of complex, which means it is allowed to use internals of that object. It is not part of the object though, so you can not call it via the object.
You must call fun
just like any other function, which it is.
Example:
void fun(complex &c)
{
// fun is a friend so it can access private member x.
cout << "outside " << c.x << endl;
}
int main()
{
complex c1;
fun(c1);
return 0;
}
Upvotes: 1