Question about where to put my objects so functinos can have access to them

Hello everyone,


So need my life would be so much easier if my functions could access my objects. My
problem is my objects are in the main function and my functions, which are stored in a
class somewhere cannot access them. I can solve that problem by declaring the objects
globally but I hear that it isn't so good to practice...so is there a way functions can have
access to objects without being declared globally??? My book only shows objects being
declared in the main function. Thanks for any response!
Last edited on
You can just pass the objects into functions as you would with any other variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Test
{
	int x;
};

void Function(Test Object)
{
	Object.x = 5;
}

int main()
{
	Test Object;
	Function(Object);
}


As long as the header file is included to point to where the class is, you should be able to pass it around to any function.

(It will also be worth looking up passing by reference once you start using objects like this).
Just tried that out. Thanks because I did not know I could pass an object through a function but my problem is more on the lines of my project not even being able to be built because objects that have not been declared yet...[declared in main]...reside within my member functions and do not even know what they are. Here is a bit of my code...I have 4 objects named p1, p2, p3, and p4 and they are declared in the main function. I will be changing the 4 object into arrays but I still want to know how to get functions to recognize member class variable/objects...

Lines 60 through 80 are where the objects "were not declared"
Lines 144 through 147 are where I declared the objects


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*___________________________________"WHO GUESSED THE BEST???"___________________________________

- There will be five rounds
- The player who guesses closest to the hidden number wins
- If no one has guessed the correct answer within 5 rounds, the player with the highest score wins
- To score high points, a player has to consistently guess very close to the hidden number!
- If the player misses the hidden numnber by 1, then that player will get a hint the next time around as to what the hidden number is

*/
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

class GET_SET_CLASS/**     ______________________________ M.Y   F.I.R.S.T   C.L.A.S.S ______________________________     **/
{
    public:

        void SetPoints(int x)///set points for having a good guess
        {
                m_points += x;
                if(x==50)
                cout<<"This will be a very interesting game IF this goes through!";
        }

        int GetPoints()///returns a players points
        {
                return m_points;
        }

        void SetPlayerName()///sets the players name
        {
                cout<<"what is your name: ";
                getline(cin, m_name);
                while(m_name == "")
                {
                    cout<<"Please enter a name: ";
                    getline(cin, m_name);
                }
        }

        string GetPlayerName()
        {
                return m_name;
        }

        int EvaluatePlayersGuess(int x)//Come back to this function after I revamp the objects in this class to be arrays...HOLY SHIT!!!!
        {
                if(x == m_hiddenNumber)///if the player guesses the number
                {
                        void StatScreen();
                        return 0;
                }

                else if((x == m_hiddenNumber -1) || (x == m_hiddenNumber +1))///if players's guess is just shy of the hidden number by 1
                {
                        if(GetPlayerCurrentlyPlaying() == 1)
                            p1.SetPoints(25);
                        else if(GetPlayerCurrentlyPlaying() == 1)
                            p2.SetPoints(25);
                        else if(GetPlayerCurrentlyPlaying() == 1)
                            p3.SetPoints(25);
                        else if(GetPlayerCurrentlyPlaying() == 1)
                            p4.SetPoints(25);
                }

                else if((x >= m_hiddenNumber - 10) && (x <= m_hiddenNumber + 10) && (x != m_hiddenNumber))///if player's guess is shy by ten
                {
                        if(GetPlayerCurrentlyPlaying() == 1)
                            p1.SetPoints(10);
                        else if(GetPlayerCurrentlyPlaying() == 1)
                            p2.SetPoints(10);
                        else if(GetPlayerCurrentlyPlaying() == 1)
                            p3.SetPoints(10);
                        else if(GetPlayerCurrentlyPlaying() == 1)
                            p4.SetPoints(10);
                }
        }

        void SetRandomNumber()
        {
                m_hiddenNumber = rand() % 50 + 1;
        }

        int GetRandomNumber()
        {
                return m_hiddenNumber;
        }

        void SetPlayerCurrentlyPlaying(int x)
        {
                m_currentPlayer = x;
        }

        int GetPlayerCurrentlyPlaying()
        {
                return m_currentPlayer;
        }

        void Instructions()
        {
                cout<<"(1.) There will be five rounds\n"
                    <<"(2.) The player who guesses the hidden number wins\n"
                    <<"(3.) If game ends w/out number being guessed, player with highest score wins\n"
                    <<"(4.) To score high points, consistently guess very close to the hidden number\n"
                    <<"(5.) *** Players receives hints when they guess really close to the number ***\n";
        }

        void OutputTheNamesOfThePlayers(int x)
        {

        }


        GET_SET_CLASS()
        {
                m_hiddenNumber = 0;
                m_points = 0;
                m_currentPlayer = 0;
        }

    private:

        string m_name;///specifies the name of the character
        int m_hiddenNumber;///the random number
        int m_points;///the points of each player
        int m_numOfPlayersToPlay;///specifies how many players the user specifies will play
        int m_currentPlayer;///To let the program know that it is currently player 1's turn
        ///comment for the above variable - if 1: player 1 is currently active.....if 2: player 2 is currently active....and so on.....
};






int main()/**     ________________________________________T.H.E   M.A.I.N   F.U.N.C.T.I.O.N________________________________________     **/
{
    GET_SET_CLASS universal;///object to call everything that does not have to do with the players
    GET_SET_CLASS players[4];//drop the other 4 objects and make one array of 4 elements
    GET_SET_CLASS p1;
    GET_SET_CLASS p2;
    GET_SET_CLASS p3;
    GET_SET_CLASS p4;
Last edited on
Oh I see what you are having problems with now.

The problem is you are trying to work with class objects inside of the class they were created from (which is possible..but not needed).

Each object has it's own set of variables to work with when created so when you call a function from an object it knows which values to refer to automatically (Which is all related to the 'this' pointer...but you can ignore all that for now).

So instead of doing this.
1
2
3
4
if(GetPlayerCurrentlyPlaying() == 1)
         p1.SetPoints(25);
else if(GetPlayerCurrentlyPlaying() == 1)
         p2.SetPoints(25);


You can just call the functions from main based on the objects.
1
2
p1.EvaluatePlayersGuess();
p2.EvaluatePlayersGuess();

and change your function to
1
2
3
4
if(GetPlayerCurrentlyPlaying() == 1)
         SetPoints(25);
else if(GetPlayerCurrentlyPlaying() == 1)
         SetPoints(25);


The function will know who to give the points to based on the object that you used in main, so you don't need to directly show it.
Oh ok. Thanks very much!
Topic archived. No new replies allowed.