EDIT:
The original program has multiple files as shown in here
I'm trying to access protected data member in my class from a friend member function of a different class.
I keep getting an access error:
9:32: error: invalid use of incomplete type 'class b' 5:7: error: forward declaration of 'class b' In member function 'void b::f(a*)': 12:13: error: 'int a::i' is protected 20:47: error: within this context
This is my code:
// Example program
#include <iostream>
#include <string>
class b;
class a{
public:
friend void b::f(a* pointer);
protected:
int i = 6;
};
class b{
public:
void f(a* pointer){std::cout<<pointer->a::i<<std::endl;}
};
int main()
{
a a1;
b b1;
b1.f(&a1);
}
b::f()as afriend, it is necessary that classbALREADY have been defined. A simpleclass bdeclaration is not enough. - Peterabefore the definition of classb. Then define classbbefore classa, not after. You'll also need to move the definition ofb::f()so it is outside (not inline) the class definition. - Peter