how can i call an objects function from within a function

so i have a class with a function, my object now has this function that i could call, how could i call that from within a function?


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
class rabbits

        void rabbitfunction()
 {}

~~

};


void function ()//prototype
~
~


main ()
{

rabbits rabobject;
function()

}

void function ()
{

rabobjrcts.rabbitfunction;



}
is it not possible to call a function from an object in a function?
It is. You wither need to pass an object to that function (preferably reference or const reference) or create the object within the function itself
You can call it the following way as shown below. If the member function does not change the object itself then it could be declared with qualifier const. For example void rabbitfunction() const {}. In this case 'function' shall take a const reference to an object of the class.

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
class rabbits

        void rabbitfunction() 
 {}

~~

};


void function ( rabbits &obj )//prototype
~
~


main ()
{

rabbits rabobject;
function( rabobject );

}

void function ( rabbits &obj )
{

obj.rabbitfunction();



}
Last edited on
ah okay i didnt see this help for a while thanks, i was trying lots of different combinations of the slightly wrong things cheers guys
Last edited on
kay i did it but wheres the flaw the error is undefeined reference to player and undefined reference to baddy


i know i got my whole code up but i will comment where the pseudo code notes are; as above, so below


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
148
149
150
151
152
153
154
155






class player
{ public:
    player (int health);
    void gethit ()
    {
        health=health-10;
        cout << "lost ten health"<<" now has "<< health;
    }

    int health;
};

class baddy
{
    public:
    baddy (int health);
    void gothit()
    {

        health=health-10;
        cout << "lost ten health"<<" now has "<< health;
    }
    void ambush(player *playpoint)  ///here is the class member function//////
    {
        playpoint->gethit();
    }

    int health;
};
int posx=10;
int posy=10;

char z='*';
char x=' ';
char b='^';
char u='!';

char brdray [20] [20] = {  {z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z}
                                          };

void move ();
void getboard ();


void proximity (baddy &object1,player &object2,char *array ,int s, int j); ///the
///proto type/////////////////////////


player you (100);
baddy them (100);

bool quit = false;

int loop =0;

int main()   ////////////////////here is main////////////////
{
brdray [5] [5] = u;
    while (quit != true)
{
 system ("cls");
getboard();
move();


char *array = &brdray[0][0];

proximity(them,you,array,20,20); ///have i over complicated this
loop++;
if (loop==10)
{
    cout << "do you want to quit? (y/n)";
    char yn;
    cin >> yn;
    if (yn == 'y')
    {
        quit = true;
    }
    else if (yn=='n')
    {
        loop = 0;
    }
}
}


return 0;
}







~~
~~
~~



///////////below is the fuction////////////////


void proximity (baddy &object1,player &object2,char *array ,int s, int j)
{

    int n,m,k,l;

        n=s;
        m=j;
        n=n+1;
        m=m+1;
        k=s;
        l=j;
        l=l-1;
        k=k-1;
   for (s=0;s<20;s++)
   {
       for (j=0;j<20;j++)
       {
           if (brdray [s] [j]=='^'&&brdray [s+1][j]=='!')
           {
                object1.ambush(&object2);///this is the call
           }

       }
   }
}
Last edited on
Obviously, this is not the code you are compiling. It has no includes, no using statement and:

1
2
3
~~
~~
~~


isn't valid C++.

Please present a minimal, compilable snippet that illustrates the problem.
i will take everything that it doesnt need for a snippet out...i didnt realize that this is what a snippet is, however, i think i got to keep it all in because of the amount of parameters the code takes in function

here it is, there are some loose bits because i havnt finished tidying it up and building other bits

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>


using namespace std;

class player
{ public:
    player (int health);
    void gethit ()
    {
        health=health-10;
        cout << "lost ten health"<<" now has "<< health;
    }

    int health;
};

class baddy
{
    public:
    baddy (int health);
    void gothit()
    {

        health=health-10;
        cout << "lost ten health"<<" now has "<< health;
    }
    void ambush(player *playpoint)
    {
        playpoint->gethit();
    }

    int health;
};
int posx=10;
int posy=10;

char z='*';
char x=' ';
char b='^';
char u='!';

char brdray [20] [20] = {  {z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,z},
                                        {z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z,z}
                                          };

void move ();
void getboard ();
void proximity (baddy &object1,player &object2,char *array ,int s, int j);



bool quit = false;

int loop =0;

int main()
{

player you (100);
baddy them(100);


brdray [5] [5] = u;
    while (quit != true)
{
 system ("cls");
getboard();
move();


char *array = &brdray[0][0];

proximity( them,you,array,20,20);
loop++;
if (loop==10)
{
    cout << "do you want to quit? (y/n)";
    char yn;
    cin >> yn;
    if (yn == 'y')
    {
        quit = true;
    }
    else if (yn=='n')
    {
        loop = 0;
    }
}
}


return 0;
}

void getboard ()
{
    for (int a=0;a<20;a++)
    {
        for (int b=0;b<20;b++)

            cout<<brdray [a][b];
            cout<<endl;

    }
}

void move ()
{

int enter;
int a,e,i,o;


brdray [posx] [posy] = x;

a=posx;
i=posx;
e=posy;
o=posy;
a=a+1;
e=e+1;
i=i-1;
o=o-1;


cout << "enter 1,2,3, and 4 to move" <<endl;
cin >> enter;
switch (enter)
{
case 1:
if(brdray [a][posy]!='*')
posx=posx+1;
break;
case 2:
if (brdray [posx][o]!='*')
posy=posy-1;
break;
case 3:
if (brdray[posx][e]!='*')
posy=posy+1;
break;
case 4:
if (brdray [i][posy]!='*')
posx=posx-1;
break;
}
brdray [posx] [posy] = b;
}
void proximity (baddy &object1,player &object2,char *array ,int s, int j)
{

    int n,m,k,l;

        n=s;
        m=j;
        n=n+1;
        m=m+1;
        k=s;
        l=j;
        l=l-1;
        k=k-1;
   for (s=0;s<20;s++)
   {
       for (j=0;j<20;j++)
       {
           if (brdray [s] [j]=='^'&&brdray [s+1][j]=='!')
           {
             object2.ambush(&object1);
           }

       }
   }
}


i think learning this will close so many holes in my knowledge

edit 5; keep forgetting to copy it properly this is up to date with the error message undefined reference tobaddy...(then the same but plyer instead of baddy)
Last edited on
You've failed to produce a minimal or compilable code snippet.

How do I know if it's compilable, you ask? You attempt to compile it and verify that the problem is still occuring.
yes but what if i take something out that is part of the thing i dont know is causing the problem!? for example i have to leave that big clunky array in because its actually in the function in questions arguments, oh i could write a new code that produces the same problem? sry i on it now
yes but what if i take something out that is part of the thing i dont know is causing the problem!?


Then you've begun isolating the problem.
i discovered this while typing it -_-
you know cire i was thinking that there was a problem because i had to say something about a constructor for the class object in the functions parameters,

turns out that i just wrote the constructor wrong and the couldn't tell where the problem was, i feel foolish now but i guess its actually an invaluable lesson worth passing on, you know whenour parents talked about the fool who did this or that in stories, the fool taught us every thing we know XD
Last edited on
Topic archived. No new replies allowed.