Access specifier error

When i compile this program
//This program is for making a whole class friend & other stuffs.
#include <iostream>
#include <string>
using namespace std;
class First
{
int a;
string s;
public:
void FirstInitializer (string);
friend class Second;
friend void ThirdFunction(First*);
};
class Second
{
int b;
public:
void SecondInitializer();
void mul (First*);
};
class Third
{
string t;
public:
void ThirdInitializer(string);
void ThirdFunction(First* f)
{
cout << t << "\n" << f->s;
}
};
void First::FirstInitializer(string g)
{
a=50;
s=g;
}
void Second:: SecondInitializer()
{
b=7;
}
void Third::ThirdInitializer(string h)
{
t=h;
}
void Second::mul(First* ft)
{
cout << "This is a&b:" << ft->a << "\n" << b << "\n";
}
int main()
{
First fi;
Second se;
Third th;
string w,x;
w="This part is within first class.";
x="This part is within second class";
fi.FirstInitializer(w);
th.ThirdInitializer(x);
se.SecondInitializer();
se.mul(&fi);
th.ThirdFunction(&fi);
}
I get the error that in member function cpp:8:'void Third::ThirdFunction(First*)':
''std::string First::s' is private.
cpp:28:within this context
closed account (o3hC5Di1)
Hi there,

You are passing that function an object of class First, let's look at it's definition:

1
2
3
4
5
6
7
8
9
10
class First
{
    int a;
    string s;

    public:
        void FirstInitializer (string);
        friend class Second;
        friend void ThirdFunction(First*);
};


string s; is specified above the public-keyword. By default all members of a class are private, so s is private.

If you want to access it from a function of another class, either add that class to First as a friend (as you're doing with Second, or make that member public.

Hope that helps.

All the best,
NwN
TThanx a lot friend.
Topic archived. No new replies allowed.