No, the problem is main doesn't know what dim is cause it's an attribute of Sina_NS::Sina objects.
PS: The interfaces of your classes collide - both have the public int attributes width and height. Because Sina already has those two, there is no need to declare them again in Dim.
#include <iostream>
usingnamespace std;
namespace Sina_NS {
class Dim;
class Sina {
public:
int width, height;
class Dim *dim;
int area () {return (width * height);}
};
}
usingnamespace Sina_NS;
class Dim: public Sina{
public:
int width, height;
int pp () {return (width * height);}
};
int main () {
dim->width=2;
return 0;
}
That should do it. It seems that the behavior described by filipe did not hold with your compiler. Maybe it is compiler-specific. Go with this way instead.
But just so you know, if it had worked, the full name of the class would have been Sina_NS::Sina::Dim and not Sina_NS::Dim.
Also note that your declaration of the Dim class is outside the Sina_NS namespace, meaning its full name is just Dim.
hanst99 is correct. A forward declaration only tells the compiler about a name. If you need to dereference a pointer (access the object it points to), then you need a full declaration.
There is no real advantage, the only reason to ever do that would be a cross reference between classes (Class A has a B object, and Class B has a A object). And even then I'd rather put a class B; forward declaration in front of A instead. But you CAN do it, so if you feel like it, do it. It's not like you'd break anything.