Setting class variable

1
2
3
4
5
6
7
8
9
10
for (int x=0; x<7; x++)
{ //Setting x and y values of tiles
    for (int y=0; y<5; y++)
    {
        tile[x][y].x = x;
        tile[x][y].y = y;
        tile[x][y].parent->x = -1; //<- crashes here with EXC_BAD_ACCESS
        tile[x][y].parent->y = -1;
    }
}

For some reason the above code crashes, im just trying to show that the parent of each tile is uninitialized. I have no reason why it would crash. I tried to set x, and y -1 in the constructor of the class but it also crashes
Are you sure that parent in the expression tile[x][y].parent->x has a valid value?
What do you mean ? Heres the whole 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
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

//
//  tile.h
//  Allegro
//
//  Created by Angel on 8/29/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <math.h>
#include <iostream>
using namespace std;

#ifndef Allegro_tile_h
#define Allegro_tile_h

class tiles {
public:
    tiles() { type = 0;g=10; g=0; h=0; f=0;}
    //~tiles() { delete parent; }
    void set(int value) { if (value == 0 || value == 1 || value == 2 || value == 3) type = value; }
    int getX(int x) { return x*64; }
    int getY(int y) { return y*64; }
    void setX(int tX) { parent->x = tX; } //Still crashes
    void setY(int tY) { parent->y = tY; } //Still crashes
    tiles *parent;
    int g,h,f;
    int x,y;
    int type;
    int getG()
    {
        /*if ((parent->x < x && parent->y == y) || (parent->x > x && parent->y == y) || (parent->y > y && parent->x == x) || (parent->y < y && parent->x == x)) {
            return this->g = 10;
        }
        return this->g = 14; */
        return g = 10;
    }
    int getH(tiles &end)
    {
        int temp1 = (abs(this->x - end.x));
        int temp2 = (abs(this->y - end.y));
        return this->h = 10*(temp1 + temp2);

    }
    int getF(tiles &end)
    {
        return f = (getG() + getH(end));
    }
    tiles& operator= (tiles &temp)
    {
        // do the copy
        this->x = temp.x;
        this->y = temp.y;
        
        // return the existing object
        return *this;
    }
    bool operator!= (tiles &temp)
    {
        return !(this->x == temp.x && this->y == temp.y);
    }
    bool operator == (tiles &temp)
    {
        return (this->x == temp.x && this->y == temp.y);
    }
private:
};




#endif 


I know this codes pretty ugly, i needa fix it soon :p
Last edited on
In the constructor of the class

tiles() { type = 0;g=10; g=0; h=0; f=0;}

you do not set an initial value for member

tiles *parent;

So I am asking Are you sure that parent has a valid value?
No im not. How would i set it to something? I cant just set the x and y values?
It is your program. I do no know what you are trying to do. I think that this member shall point to some other tiles that is it shall contain a valid address of another tiles.
I fixed it, i just took out the parent and replaced it with x and y variables
Thanks for your help tho :)
Topic archived. No new replies allowed.