Nested Classes in C++: Protected Access Error

  • Context: C/C++ 
  • Thread starter Thread starter dodo
  • Start date Start date
  • Tags Tags
    C++ Classes
Click For Summary

Discussion Overview

The discussion revolves around issues related to accessing protected members of nested classes in C++. Participants are exploring the specific error encountered when implementing a function within a nested structure, particularly in the context of using Microsoft Visual C++ 6.0.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a scenario where a nested structure (PartTwo) cannot access another nested structure (PartOne) due to access restrictions, despite attempts to declare it as a friend.
  • Another participant suggests that the issue may be related to known problems with the Microsoft compiler, recommending an upgrade or a switch to GCC.
  • A later reply confirms that the same access error occurs in a provided example code, reinforcing the idea that the compiler may be at fault.
  • Some participants express frustration with the limitations of the Microsoft compiler, suggesting it is not ideal for such tasks.
  • One participant indicates that they are constrained to use the VC++ 6.0 compiler due to corporate decisions, despite recognizing its flaws.

Areas of Agreement / Disagreement

Participants generally agree that the issue is likely related to the Microsoft compiler's handling of nested classes, but there is no consensus on a definitive solution, as some suggest workarounds while others express frustration with the compiler itself.

Contextual Notes

Limitations include the specific behavior of the Microsoft VC++ 6.0 compiler, which may not align with standard C++ behavior regarding access to protected members in nested classes.

dodo
Messages
695
Reaction score
2
Hopefully this sort of questions will soon be answered in the tutorial (good effort, Dduardo!), but for the time being, this one is not.

I have the following scheme:

class Foo {
...
protected:

struct PartOne {
...
};

struct PartTwo {
...
PartOne* someFunction ();
};​
};
And I get a complaint of PartTwo's being unable to access a protected symbol (PartOne) from Foo.

A "friend struct PartTwo;" right after the "protected:" indication doesn't seem to solve the problem, as I expected. (I'd prefer not to take the two Parts out of Foo, as independent structures.) Where am I thinking wrong?

Thanks very much in advance.

---
Edit: If this helps... actually I'm not getting the error in the declaration (like the above), but when later writing the implementation of 'someFunction', which spells like:

Foo::PartOne* Foo::PartTwo::someFunction () &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// <--- offending line
{
...
}​
The MS VC++ 6.0 compiler says,
'PartOne' : cannot access protected struct declared in class 'Foo'
 
Last edited:
Technology news on Phys.org
You might be running into the infamous nested problems with the Microsoft compiler. You'll need to either upgrade to version VC++ 7.0 or use GCC.

Here is some example code that works under GCC:

Code:
#include <iostream>

using namespace std;

class foo {

protected:

        struct foo1 {
                int x;
        };

        struct foo2 {
                foo1 myfunction(void) ;
        };

public:
        void myfunction2(void) ;

};

void foo::myfunction2(void) {
foo2 test ;
test.myfunction();
}

foo::foo1 foo::foo2::myfunction(void) {
cout << "hello\n";
}

int main (void) {

foo testme;

testme.myfunction2();

}
 
I should've suspected when the compiler issued the error message TWICE for the same line. :)

Thank you; I'll try to workaround it.

---
Edit:
Confirmed - I get the same error (thrice :)
error C2248: 'foo1' : cannot access protected struct declared in class 'foo'
on line 27 of your program,
foo::foo1 foo::foo2::myfunction(void) {
Thanks for crosschecking.
 
Last edited:
Microsoft makes great mice and keyboards, but stay away from their compiler like the plague.
 
Dodo said:
I should've suspected when the compiler issued the error message TWICE for the same line. :)

Thank you; I'll try to workaround it.

Is there some reason you have to use the VC++ 6.0 compiler? It pains me to see someone work around a compiler flaw; particularly for such a horrible compiler.
 
It's the one at hand, and my workplace's choice. Nothing I can't do. (Corporate decisions here.) Thanks for caring.

Edit: typo.
 
Last edited:

Similar threads

  • · Replies 3 ·
Replies
3
Views
16K
  • · Replies 13 ·
Replies
13
Views
21K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 6 ·
Replies
6
Views
9K
  • · Replies 12 ·
Replies
12
Views
5K