Nested Classes in C++: Protected Access Error

In summary, The conversation is discussing a problem with accessing protected symbols in a code structure. The issue seems to be related to the Microsoft compiler and there is a suggestion to use a different compiler or upgrade to a newer version. There is also a code example provided that works in GCC but not in the Microsoft compiler. Finally, it is mentioned that the person must use the Microsoft compiler due to their workplace's decision.
  • #1
dodo
697
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
  • #2
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();

}
 
  • #3
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:
  • #4
Microsoft makes great mice and keyboards, but stay away from their compiler like the plague.
 
  • #5
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.
 
  • #6
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:

1. What is a nested class in C++?

A nested class in C++ is a class that is declared within the scope of another class. This allows for the nested class to have access to the private members of the outer class, while also providing encapsulation and organization within the overall class structure.

2. How do you declare a nested class in C++?

To declare a nested class in C++, you simply place the inner class declaration within the scope of the outer class, using the keyword "class" followed by the class name. For example:
class OuterClass {
  class InnerClass {
    // inner class members and functions
  }
  // outer class members and functions
};

3. Can a nested class access private members of the outer class?

Yes, a nested class can access the private members of the outer class. This is because the nested class is considered a member of the outer class and has access to all of its members, including private ones.

4. What is the difference between a nested class and a nested struct in C++?

In C++, a nested class and a nested struct are essentially the same, with the only difference being the default access specifier. Nested classes default to "private" access, while nested structs default to "public" access. However, this can be changed by using the "class" or "struct" keywords before the nested class or struct declaration.

5. Can a nested class be declared as a friend of the outer class?

Yes, a nested class can be declared as a friend of the outer class, just like any other class. This allows the nested class to access private members of the outer class, even if they are not accessible to other classes.

Similar threads

  • Programming and Computer Science
Replies
3
Views
15K
  • Programming and Computer Science
Replies
13
Views
19K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
8K
Back
Top