I'm trying to access the private data members of a struct from a friend class as shown below. All the code is in a single cpp file:
namespace Foo
{
struct TestStruct
{
friend class Bar;
private:
int _testNum;
};
}
class Bar
{
public:
Bar();
private:
Foo::TestStruct _testStructObj;
};
Bar::Bar()
{
_testStructObj._testNum = 10; //Cannot modify _testNum because it is private
}
On compilation, I get an error saying that _testNum in the struct TestStruct is private hence inaccessible. After trying different things and searching the web I finally decided to remove the namespace and the code compiles. Why can't I access the private data members of the struct from a friend class when the struct is defined in a namespace?