Help with assigning values to Vector inside Class

I'm attempting to amend a GRID class I created awhile back to work with a path finding class I'm working on now. Basically, everything works, except for the _moveCost[] vector keeps resetting and I can't figure out why.

Here is the Grid header file:
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
#ifndef GRID_H
#define GRID_H

#ifndef RECTANGLE_H
#include "rectangle.h"
#endif

#include <vector>

enum Direction { NORTH, SOUTH, EAST, WEST, NORTHEAST, NORTHWEST, SOUTHEAST, SOUTHWEST };

class grid{
public:
	grid(){};
	class cell{
		friend class grid;
	public:
		cell() :_index(0) {}
		
		int Index() { return _index; }
		int Width() { return _cWidth; }
		int Height() { return _cHeight; }

		point Point();
		point Center();
		void SetMoveCost(float);
		float GetMoveCost();

		rectangle Rectangle();
		int Neighbor(Direction Dir);

		int Row();
		int Column();

		cell operator[](int index) 
 		{ 
			if ((index < 0) || (index > ((_gColumns*_gRows)-1)))
			{ _index = -1; return *this; }
			_index = index; 
			return *this; 
		}  
		cell operator[](point p) 
		{ 
			if ((p.X < 0) || (p.Y < 0) || (p.X > (_gColumns*_cWidth)) || (p.Y > (_gRows*_cHeight)))
			{ _index = -1; return *this; }
		
			if ((_gColumns*_gRows) == 1) { _index = -1; return *this; }
			
			int thisCol = int(p.X) / _cWidth;
			int thisRow = int(p.Y) / _cHeight;
			_index = (thisRow * _gColumns) + thisCol;
			return *this;
		}
	private:
		int _index;
		int _gRows;
		int _gColumns;
		int _cHeight;
		int _cWidth;
		std::vector<float> _cMoveCost;
	}Cell;

	void Create(int Rows, int Columns, int CellsWidth, int CellsHeight);
	
	bool Exists() { return _exists; }
	int Width() { if (_exists == false) return -1; return (Cell._gColumns * Cell._cWidth); }
	int Height() { if (_exists == false) return -1; return (Cell._gRows * Cell._cHeight); }
	int Size() { if (_exists == false) return -1; return (Cell._gColumns * Cell._gRows); }

private:
	bool _exists;
};

void grid::Create(int Rows, int Columns, int CellsWidth, int CellsHeight)
{
	_exists = false;
	if ((Rows > 0) && (Columns > 0) && (CellsWidth > 0) && (CellsHeight > 0))
	{
		_exists = true;
		Cell._gRows = Rows;
		Cell._gColumns = Columns;
		Cell._cWidth = CellsWidth;
		Cell._cHeight = CellsHeight;
		Cell._index = 0;
		Cell._cMoveCost.resize(Size(),0.0);
	}
}

point grid::cell::Point()
{
	point p(-1,-1);
	if (_index < 0 || _index > ((_gColumns*_gRows)-1)) return p;
	//Solve for X
	p.X = float(_index);
	if (p.X >= _gColumns) p.X = float(int(p.X) % _gColumns);
	p.X *= float(_cWidth);
	//Solve for Y
	p.Y = float((_index / _gColumns) * _cHeight);
	_index = 0;
	return p;
}

point grid::cell::Center()
{
	point p = Point();
	if ((p.X == -1) && (p.Y == -1)) return p;
	p.X += (_cWidth/2);
	p.Y += (_cHeight/2);
	return p;
}

rectangle grid::cell::Rectangle()
{
	rectangle r(-1,-1,-1,-1);
	point p = Point();
	if ((p.X == -1) && (p.Y == -1)) return r;
	r.P1 = p;
	r.P2.X = r.P1.X + _cWidth -1;
	r.P2.Y = r.P1.Y + _cHeight -1;
	return r;
}

int grid::cell::Neighbor(Direction Dir)
{
	int n = -1;
	if (_index < 0 || _index > ((_gColumns*_gRows)-1)) return n;

	switch (Dir)
	{
	case NORTH:
		n = _index - _gColumns;
		if (n < 0) n = -1;
		break;
	case SOUTH:
		n = _index + _gColumns;
		if (n > ((_gColumns*_gRows)-1)) n = -1;
		break;
	case EAST:
		n = _index + 1;
		if ((int(_index)+1) % int(_gColumns) == 0) n = -1;
		break;
	case WEST:
		n = _index -1;
		if (int(_index) % int(_gColumns) == 0) n = -1;
		break;
	case NORTHEAST:
		n = _index - (_gColumns - 1);
		if ((n<0) || (int(_index) % int(_gColumns) == 0)) n = -1;
		break;
	case NORTHWEST:
		n = _index - (_gColumns + 1);
		if ((n<0) || (int(_index) % int(_gColumns) == 0)) n = -1;
		break;
	case SOUTHEAST:
		n = _index + _gColumns + 1;
		if ((n > ((int(_gColumns)*_gRows)-1)) || ((int(_index)+1) % int(_gColumns) == 0)) n = -1;
		break;
	case SOUTHWEST:
		n = _index + (_gColumns-1);
		if ((int(_index) % int(_gColumns) == 0) || (n > ((_gColumns*_gRows)-1))) n = -1;
		break;
	default:
		n = -1;
		break;
	} //end of switch
	_index = 0;
	return n;
}

int grid::cell::Row()
{
	point p = Point();
	if ((p.X == -1) && (p.Y == -1)) return -1;
	return (int(p.Y) / _cHeight);
}

int grid::cell::Column()
{
	point p = Point();
	if ((p.X == -1) && (p.Y == -1)) return -1;
	return (int(p.X) / _cWidth);
}

void grid::cell::SetMoveCost(float moveCost)
{
	if (_index >= 0 && _index <= ((_gColumns*_gRows)-1))
	{
		_cMoveCost.at(_index) = moveCost;
	}
}

float grid::cell::GetMoveCost()
{
	if (_index >= 0 && _index <= ((_gColumns*_gRows)-1))
	{
		return _cMoveCost[_index];
	}
	return -1;
}
#endif 


And here is the "tester code" I'm trying to get to work as console output. What SHOULD happen is "5" is printed to the console window. Instead, all I get is "0", which is what the _moveCost[] vector is set to by default in this line above: Cell._cMoveCost.resize(Size(),0.0);.

I know it's being setup properly above, cause when I change the 0.0 to something else in the grid.h file, the new value prints on the console window.

The issue is that the GetMoveCost() below will not return the value set in the SetMoveCost() OR the SetMoveCost() is not changing the value:

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
#include <iostream>
#include "../../../../Libraries/grid.h"
#include <cmath>


using namespace std;


int main()
{
	grid g;
	g.Create(5,6,10,10);

	for (int i=0; i<g.Size(); ++i)
	{ 
		g.Cell[i].SetMoveCost(5.0);	
	}

	cout << g.Cell[3].GetMoveCost() << endl;



	cout << endl << endl;
	system("pause");
	return 0;
}


The rectangle.h header is not being used for what I'm testing. I didn't include it due to space. I promise that isn't the issue.
Topic archived. No new replies allowed.