# MinGW Has No Friends :P

## Recommended Posts

webwraith    239
Hi, I'm trying to include nested classes in my program, but when I try to compile, the compiler gives me the error;
a class-key must be used when declaring a friend
Here are the first two classes (the main manager class, and one of its nested classes)
class GUI_MANAGER{
public:
GUI_MANAGER();
void Update();//called each frame. self-explanatory
GUIuint RegisterClass(HPARENT);//registers a class to the manager
void PostMessage(GUIuint,HPARENT);//sends message GUIuint, to window HPARENT

protected:
class INT_WINDOW;//holds each window passed to the manager by RegisterClass()
class GUI_SCREEN;//a derivation of GUI_RECT, set to the screen coordinates

vector<INT_WINDOW> window_list;// a vector that holds every window
vector<INT_WINDOW>::iterator wnd_it;//vector iterator for travelling around window_list

};

class GUI_MANAGER::INT_WINDOW{
private:
friend GUI_MANAGER;

HPARENT window;
bool visible;
bool active;

INT_WINDOW(){}
~INT_WINDOW(){}
};

Also, I tried to derive GUI_SCREEN from GUI_RECT (not shown) with the line;
class GUI_MANAGER::GUI_SCREEN : GUI_RECT{
but it gives an error of
invalid use of undefined type "struct GUI_RECT"
along with another error of
forward declaration of "struct GUI_RECT"
Well, I did forward declare GUI_RECT, but it's a class,not a struct, and I don't get the second error unless I try to derive GUI_SCREEN from GUI_RECT as always, can anyone give me a hand with this because I just can't work out what's wrong

##### Share on other sites
Enigma    1410
For the first problem, both functions and classes can be made friends. You must tell the compiler that you want the GUI_MANAGER class to be made a friend:
class GUI_MANAGER::INT_WINDOW{    private:        friend class GUI_MANAGER;

For the second, is GUI_RECT also a nested class within GUI_MANAGER? If so you will need to qualify it's name:
class GUI_MANAGER::GUI_SCREEN : GUI_MANAGER::GUI_RECT{

For the third (which you didn't ask), you are aware that classes default to private inheritance, right? So class GUI_SCREEN : GUI_RECT is equivalent to class GUI_SCREEN : private GUI_RECT. If you want public inheritance (is-a) instead of private inheritance (is-implemented-in-terms-of) then you need to explicitly use public inheritance.

For the fourth (which you also didn't ask), all-caps names are usually reserved for preprocessor macros. It is potentially dangerous to use them for class names (you increase your chance of name collisions).

Σnigma

##### Share on other sites
bakery2k1    712
friend class GUI_MANAGER

You're missing the "class".

EDIT: Beaten

##### Share on other sites
webwraith    239
Thanks for the help. No, GUI_RECT isn't nested in GUI_MANAGER, it's a separate class. One other thing, when you specify, say;

class Derived:public Base

does this mean that Derived has a copy of both protected and public methods and variables, or just the public ones?

##### Share on other sites
jpetrie    13159
Derived has a copy of everything in Base (even things that are private). However, it can access only the public and protected members of Base.

##### Share on other sites
Zahlman    1682
Quote:
 Original post by webwraithThanks for the help. No, GUI_RECT isn't nested in GUI_MANAGER, it's a separate class. One other thing, when you specify, say;class Derived:public Basedoes this mean that Derived has a copy of both protected and public methods and variables, or just the public ones?

Derived has a copy of public, protected and private data, regardless of the type of inheritance. Regarding the "methods" (member functions), the question doesn't make sense, because there is only one copy of code in memory for all objects of a given class; functions only "belong" to an object in a conceptual sense. (In cases where virtuals are used, each object has a vtable pointer, but the number of vtable pointers in an object depends on the complexity of the inheritance tree, not its depth.)

Rather, 'protected' and 'private' are about *access*; they are there so that the compiler can prevent you from doing things you shouldn't, like directly accessing data that's part of some abstraction.

##### Share on other sites
Guest Anonymous Poster
AP's have no friends... maybe MinGW will be my friend? [inlove]

Anon is the loneliest number that you'll ever do... [sad]

##### Share on other sites
webwraith    239
^ ??

Anyway, I understand the idea of access, what I meant to say is what does the derived class have access to when you specify protected and public (I'm assuming public gives access to both, while protected only gives access to protected methods. I've never actually been told)

[Edited by - webwraith on April 24, 2006 1:50:16 PM]

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by webwraithAnyway, I understand the idea of access, what I meant to say is what does the derived class have access to when you specify protected and public (I'm assuming public gives access to both, while protected only gives access to protected methods. I've never actually been told)

Public inheritance will make all public members of the base class public in the derived class also, protected inheritance will make all the public members of the base class protected in the derived class, private inheritance will make all public members of the base class private in the derived class.

##### Share on other sites
webwraith    239
Thanks for clearing that up AP(yep, all of you!!![wink])

does this mean that the protected members of the base class are unaccessable by the derived class, or they just retain their accessability and are included in the derived class as is?

Anyone?

[Edited by - webwraith on April 27, 2006 1:10:27 PM]

##### Share on other sites
webwraith    239
Does noone know the answer to my last post, or is there another reason for the lack of reply?