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:
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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