Class wihtin a class passed to a function in the primary class? Help!

Hey, need some help with passing my 2nd class to a function in the first class.
(Can't write all mt code cus it's HUGE! I'll just summarize!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//***Classes***//
class Game{
    public:
        class Button{
            private:
            SDL_Rect *TempButtonCut;
            SDL_Rect Box; // <=--***** I wanna pass this!! *****
            public:
            Button(int x, int y, int w, int h);
            void HandleButtonEvent();
        };

        SDL_Surface *Screen;

        SDL_Surface *MenuBackground;
        SDL_Surface *MenuButtonNewGame;
        SDL_Surface *MenuButtonLoadGame;
        SDL_Surface *MenuButtonControls;
        SDL_Surface *MenuButtonSettings;
        SDL_Surface *MenuButtonQuit;

        SDL_Surface *Level1;
        bool Init();
        void Controls();
        void Update(Button *NewGameButton); // <=-- Into Here! Not sure if thats correct so far!
        void Display();
        void Quit();
};

void Game::Update(Button *NewGameButton) //  <=-- Here is the Function!
{

    PutImage(0, 0, MenuBackground, Screen);

//                 This is Fixed now!

    PutImage(NewGameButton->Box.x, Button.NewGameButton.Box.y, MenuButtonNewGame, Screen, Button.NewGameButton.TempButtonCut); // <=-- This is where I can't work out

    PutImage(Box.x, Box.y, MenuButtonLoadGame, Screen, TempButtonCut);
    PutImage(Box.x, Box.y, MenuButtonControls, Screen, TempButtonCut);
    PutImage(Box.x, Box.y, MenuButtonSettings, Screen, TempButtonCut);
    PutImage(Box.x, Box.y, MenuButtonQuit, Screen, TempButtonCut);
}

int main(int argc, char *args[])
{
    Game Game; //<=-- Declearing

    Game::Button NewGameButton(320, 315, 640, 100); // <=-- Declaring

//************New Problem********************************

    Game.Update(Game::Button *NewGameButton, Game::Button *LoadGameButton, Game::Button *ControlsButton, Game::Button *SettingsButton, Game::Button *QuitButton); // <=-- New Problem!! ***************

//************************************************************** 


Extreme thanks if you can work it out & thanks for trying if you can't! :)
Last edited on
What seems to be the problem? Other than using period (.) instead of -> inside Game::Update(), that is.
Ahh okay! Because all of the periods (.) are working fine in the function apart from line 37!
It keeps saying expected primary experssion before "."
I tryed changing it to
 
PutImage(Button->NewGameButton.Box.x.... etc


but still nothing!
What should I change?
(Thanks!)
PutImage(NewGameButton->Box.x, ...);
You don't seem to know when or how to use operator ->. I would start reading about pointers since you seem to lack the expertise.

From whatever little code you posted, I can see you need to use operator -> instead of the period for NewGameButton ONLY, and get rid of the class name in the call to the button's methods/fields. Example: PutImage(NewGameButton->Box.x, ..... That should be it unless you are getting other errors. If you are, post them here.
Wonderful!
Works Perfectly! :D
Thanks So Much!
I understand them a bit!
I know "->" is only used for pointers! my mind just got a little confused when I started experimenting with a class in a class & passing it to the other class!
Thanks very much for your help! :)
closed account (zb0S216C)
You never instantiated Button. You need to instantiate it before you can use it (unless you're defining methods and static members within it). For example:

1
2
3
4
5
6
7
8
9
10
class Game
{
    public:
        class Button
        {
            // ...
        } _Button; // Instantiation.

        // ...
};

Use the instantiation to access the members of Button.

Wazzak
Thanks for all the help guys!
One more prob now! In the main fuction, how to I call the function with the classes passed to it?

 
Game.Update(Game::Button *NewGameButton, Game::Button *LoadGameButton, Game::Button *ControlsButton, Game::Button *SettingsButton, Game::Button *QuitButton);


That says expected primary expression before "*", & if I remove the "Game::", it says the "Button...etc" is not declared in the scope!

Thanks again!
Again, I recommend reading about pointers because you don't know how to pass them. Read http://www.cplusplus.com/doc/tutorial/pointers/ .

To answer the question, you do Game.Update(&NewGameButton, ...);
Your Wonderful, thank u! :)
& I appricate the link! I'll defo read more about it! pointers, attributes about classes like contructers, destructers etc, are things I havent quite understood yet!
Topic archived. No new replies allowed.