what is the most simple as possible program you could build that uses this evil '<-' bastard

im gonna go for the graduation in the beginner exercises on this forum http://www.cplusplus.com/forum/articles/12974/ (i shud get a trophy if i succeed)
i just need to review and practice everything...the biggest issue standing in the way of my learning is with this guy '->'

it symbolises my ongoing uphill struggle with pointers and references as well as the more complicated side of using object functions on other objects


i have not managed to wrap my head around pointers and references and more so calling object functions within other functions (by this i mean class objects with built in functions being called within separate functions, if thats possible)

maybe you could help me write the most simple as possible program that uses the -> fellah?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
struct A{
    int a;
};

int main(){
    A a;
    A *p=&a;
    p->a=10;
    (*p).a++;
    (p->a)++;
}
thnks thats good to play with

anyhelp withcalling object functions within other functions (by this i mean class objects with built in functions being called within separate functions, if thats possible)

you guys have done enuff though and what im asking for there is a big enuff program even in its most simplest form
Last edited on
I am a bit unclear about what you mean by calling a function within a separate function...

Does this help?

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
#include <iostream>
#include <string>
using namespace std;

class Hello {
public:
    void display(ostream& os) const {
        os << "Hello";
    }
};

class World {
public:
    void display(ostream& os) const {
        os << "World";
    }
};

class Message {
private:
    Hello* m_pHello;
    World* m_pWorld;

public:
    Message() : m_pHello(new Hello)
              , m_pWorld(new World) {
    }

    ~Message() {
        delete m_pHello;
        delete m_pWorld;
    }

    void display(ostream& os) const {
        m_pHello->display(os);
        os << " ";
        m_pWorld->display(os);
        os << "!" << endl;
    }
};

int main() {
    Message* pMessage = new Message;
    pMessage->display(cout);
    delete pMessage;

    return 0;
}
Last edited on
yeah it really does
Why cant i create a messages object but i can a world or hello object???

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
#include <iostream>
#include <string>

using namespace std;

class hello{
public:
void message1 ()
{
    cout << "hello";
}



};

class world{
public:
void message2()
{
    cout << "world";
}
};


class messages{
public:
hello *hellopointer;
world *worldpointer ;

void show ()
{
hellopointer->message1();
worldpointer->message2();
}

};

int main()

{
world worobj; //fine for some reason a world object is fine despite near identicle syntax to a...
messages mesobj;  //messages object nooo it wont have that

return 0;
}
What do you mean, "I can't create"? Does it give you an error? Does it crash?
oh sry yeah an error

but i tell you what this crashes...

its the fight function...something wrong with myunderstanding of pointers there...

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>


using namespace std;















class player
{
public:



int gethit ()
{
    health=health-10;
    if (health<10)
    {
        cout << "your  dead innit"<<endl;
        return 1;
    }
}
 int health;
 string name;
 int attackpower;
 int dice;
 char you;
};

class baddy
{
public:



int givehit ()
{
    bealth=bealth-10;
    if (bealth<10)
    {
        cout << "you got the wanker"<<endl;
        return 2;
    }
}
 int bealth;
 string bame;
 int battackpower;
 int bosx;
 int bosy;
 char bad;
 int bice;
};

int getrandom ();
void fight ();

char z = '*';
char x = ' ';

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

int bosx=2;
int bosy=2;
char them = '!';
char you = 'O';

void start ();
void getbrd ();
void bdymove ();
void move ();

int posx = 5;
int posy = 5;








string enter;

int main ()
{
cout << "enter your name contender 1" <<endl;
cin >> enter;
system ("cls");
baddy estateagent;
estateagent.bame="barry";

estateagent.bealth=20;
player playa;
playa.name=enter;

playa.health=100;



start();
while (1)
{

getbrd();
move();
bdymove();
fight();
system ("CLS");
}
return 0;
}












void getbrd ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }

}

void move ()
{
    int mv;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    brdray [posx] [posy] = x;
    int chekposx = posx;
    int chekposy = posy;
    int chukposx=posx;
    int chukposy=posy;
chekposx=chekposx-1;
chekposy=chekposy-1;
chukposx=chukposx+1;
chukposy=chukposy+1;
    switch (mv)
    {


    case 1:
    if (brdray[chekposx] [chekposy]!=z)
    {posx=posx-1;}
    break;
    case 2:
    if (brdray[chukposx] [chukposy]!=z)
    {posx=posx+1;}
     break;
    case 3:
    if (brdray[chekposx] [chekposy]!=z)
    {posy=posy-1;}
     break;
    case 4:
    if (brdray[chukposx] [chukposy]!=z)
    {posy=posy+1;}
     break;
    }

    brdray [posx] [posy] =you;
}


void start()
{
brdray [posx] [posy]=you;
brdray [bosx] [bosy]=them;
}

void bdymove ()
{
brdray [bosx] [bosy] = x;

if (posx>bosx)
{
    bosx++;
}
if (posx<bosx)
{
    bosx--;
}
if (posy>bosy)
{
    bosy++;
}
if (posy<bosy)
{
    bosy--;
}


brdray [bosx] [bosy]=them;
}

void fight ()
{
int hilo;
int randomnum;
randomnum=getrandom();
player *playpointer;
baddy  *badpointer;
if (brdray [posx] [posy]==brdray [bosx] [bosy])
{
    cout << "you must FIGHT!! 1 to block 2 to attack"<<endl;
    cin >> hilo;
    switch (hilo)
    {
        case 1:
        cout << "you block his attack...";
        if (hilo < randomnum)
        cout <<"and hit him";
        badpointer->givehit();
        if  (hilo > randomnum)
        cout << "only to get hit when open";
        playpointer->gethit();
        break;
        case 2:
        cout << "you swing...";
        if (hilo > randomnum)
        cout << "and get the bastard";
        badpointer->givehit();
        else if (hilo < randomnum)
        cout << "you misss and he kicks you up the arse";
        playpointer->gethit();
        break;
    }

    }
}


int getrandom ()
{
int number;
 srand(time_t(0));
 number=1+(rand()%6);
 return number;
}
Last edited on
First of all there is no such an operator as <- in C/C++. So the construction <- corresponds to two operators < (less than) and - (minus).:)

@devonrevenge

In your first post after my "hello pointer wolrd" app, you are forgetting to make the pointers in your class point to something.

Here

1
2
3
4
5
6
class messages{
public:
hello *hellopointer;
world *worldpointer ;

...


you have defined two pointer members for your message class, but they have not been set to point to something. So when you call the member functions, things go horribly wrong.

You need to set a pointer to a valid address, using new or the address-of operator (&), or a function that uses one of these methods. Or to 0 (or equivalently NULL or, in C++11, nullptr).

Note that you should not try to call a method through a null (0) pointer. It just makes it easier to check whether the address has been set or not.

A pointer, at the end of the day, just points at a memory address. But if it has a type (that is, it's not void*) you are also saying what is supposed to exist at the address. Your hellopointer is saying that there's a hello object at this memory address. The compiler will try to use whatever is at that address as a hello object, even if it is just random bytes. And in some cases, the address might refer to memory which you do not have access to.

Note that memory address 0 is special. It is used to mean nowhere. C++ doesn't allow you to use this value as an actual address.

You'll see in that in my version I use new to create instances of the Hello and World class.

Try altering you code so it allocates the words...

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
class messages{
private: // these are better as privates...
    hello *hellopointer;
    world *worldpointer ;

public:
    messages() : hellopointer(0), worldpointer(0)
    {
        // allocate word objects on the heap
        hellopointer = new hello;
        worldpointer = new world;
    }

    ~messages()
    {
        // free word objects
        delete hellopointer;
        delete worldpointer;
    }

    void show ()
    {
        hellopointer->message1();
        worldpointer->message2();
    }
};
Last edited on
havnt sussed out the new operator yet
When you declare a pointer, it doesn't actually point to anything yet -- it just has a garbage value, just like an int. No object, no structure, nada. The new operator does all the work for you -- it allocates a new object of whatever type you'd like, stores it in memory, and makes the pointer actually point to it.
ooh shiny, i like that, wish i found that in the dev tutorials...could i use it in my deeply flawed fight function up there?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if (brdray [posx] [posy]==brdray [bosx] [bosy])
{
    cout << "you must FIGHT!! 1 to block 2 to attack"<<endl;
    cin >> hilo;
    switch (hilo)
    {
        case 1:
        cout << "you block his attack...";
        if (hilo < randomnum)
        cout <<"and hit him";
        badpointer->givehit();
        if  (hilo > randomnum)
        cout << "only to get hit when open";
        playpointer->gethit();
        break;
        case 2:
        cout << "you swing...";
        if (hilo > randomnum)
        cout << "and get the bastard";
        badpointer->givehit();
        else if (hilo < randomnum)
        cout << "you misss and he kicks you up the arse";
        playpointer->gethit();
        break;


im stuggling with pointers as it is, im jjust trying to make the object call a function that kills its own health, when its killed it will return something i can use to initiate another stage in the game...but i cant wrap my head round it...i getting a new perscription of brainfocusing drugs tomorow
Topic archived. No new replies allowed.