Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ question: nested classes

  1. Oct 30, 2004 #1
    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 {

    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: Oct 30, 2004
  2. jcsd
  3. Oct 30, 2004 #2


    User Avatar
    Staff Emeritus

    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 (Text):

    #include <iostream>

    using namespace std;

    class foo {


            struct foo1 {
                    int x;

            struct foo2 {
                    foo1 myfunction(void) ;

            void myfunction2(void) ;


    void foo::myfunction2(void) {
    foo2 test ;

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

    int main (void) {

    foo testme;


  4. Oct 30, 2004 #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.

    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: Oct 30, 2004
  5. Oct 30, 2004 #4


    User Avatar
    Staff Emeritus

    Microsoft makes great mice and keyboards, but stay away from their compiler like the plague.
  6. Oct 30, 2004 #5
    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.
  7. Oct 31, 2004 #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: Oct 31, 2004
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: C++ question: nested classes
  1. C question (Replies: 19)

  2. C++ question (Replies: 8)

  3. C question (Replies: 6)

  4. An easy c question (Replies: 7)