PDA

View Full Version : C++ question: nested classes


Dodo
Oct30-04, 08:33 AM
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'

dduardo
Oct30-04, 09:38 AM
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:


#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();

}

Dodo
Oct30-04, 09:56 AM
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.

dduardo
Oct30-04, 01:28 PM
Microsoft makes great mice and keyboards, but stay away from their compiler like the plague.

master_coda
Oct30-04, 03:58 PM
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.

Dodo
Oct31-04, 03:08 PM
It's the one at hand, and my workplace's choice. Nothing I can't do. (Corporate decisions here.) Thanks for caring.

Edit: typo.