C/C++ Nested Classes in C++: Protected Access Error

  • Thread starter Thread starter dodo
  • Start date Start date
  • Tags Tags
    C++ Classes
AI Thread Summary
The discussion centers on an issue with accessing a protected struct within a class in C++. The user has a class `Foo` containing two protected structs, `PartOne` and `PartTwo`, and encounters a compiler error when trying to implement a function in `PartTwo` that returns a pointer to `PartOne`. The error arises specifically during the implementation of `someFunction`, indicating that `PartTwo` cannot access `PartOne` due to its protected status. The user notes that a "friend" declaration does not resolve the issue and suspects it may be related to limitations of the Microsoft VC++ 6.0 compiler, suggesting that upgrading to a newer version or using GCC could solve the problem. The discussion highlights frustrations with the older compiler and the challenges posed by its handling of nested structures and access specifiers.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
3
Views
16K
Replies
4
Views
4K
Replies
12
Views
5K
Back
Top