Passing a vector element index

Is it possible to make the following statement work...

point x = g.Cell[2].GetPoint();

Assuming 'g' is a grid class that contains a 'Cell' vector like this...

1
2
3
4
5
class grid{
public:
friend class CELL;
  std::vector<CELL> Cell;
};


And CELL is a class that contains the function Point() like this...

1
2
3
4
5
6
7
8
class CELL{
public:
friend class grid;
  point GetPoint(){
    //int x = grid.Cell.at() <-NOPE
    //x should equal 2 in the above example
  }
};


Basically I want to pass the 2 from the first statement to the GetPoint() function since 2 is a necessary literal to calculate it's point on the grid based on it's element index (2).

Last edited on
Is this what you mean?

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

class Point
{
public:
	Point(){};
	Point(int px, int py)
	{
		x = px;
		y = py;
	}

	int x, y;

};

class Grid
{
public:
	Point GetPoint(Point xy)
	{
		return xy;
	}

	void PrintPoint(int index)
	{
		std::cout << "X : " << Points[index].x << "\n";
		std::cout << "Y : " << Points[index].y << "\n";
	}

	std::vector<Point>Points;
};

int main()
{
	Grid g;
	Point p(10, 15);

	g.Points.push_back(g.GetPoint(p));

	g.PrintPoint(0);

	while(true)
	{
	}
}
Last edited on
Not exactly.

Basically, I just want to figure out how to pass the vector's element index to a function.
This?

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

class Point
{
public:
	Point(){};
	Point(int px, int py)
	{
		x = px;
		y = py;
	}

	int x, y;

};

class Grid
{
public:
	Point GetPoint(Point xy)
	{
		return xy;
	}

	void PrintPoint(std::vector<Point>P, int index)
	{
		std::cout << "X : " << P[index].x << "\n";
		std::cout << "Y : " << P[index].y << "\n";
	}

	std::vector<Point>Points;
};

int main()
{
	Grid g;
	Point p(10, 15);
	Point p2(20, 12);

	g.Points.push_back(g.GetPoint(p));
	g.Points.push_back(g.GetPoint(p2));

	g.PrintPoint(g.Points, 0);

	while(true)
	{
	}
}
The point class really isn't the focus. But if you would like to see the point class I'm using, it's this:

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
	struct point {
		int X, Y;
		point(int X, int Y) :X(X),Y(Y) {}
		point() :X(0),Y(0) {}
		float Distance(point p)
		{
			float d = ((p.X-X) * (p.X-X)) + ((p.Y-Y) * (p.Y-Y));
			d = sqrt(d);
			return d;
		}
		void Draw(ALLEGRO_BITMAP *Surface, ALLEGRO_COLOR Color = clr.BLACK)
		{  
			if (currentSurface != Surface) { currentSurface = Surface; al_set_target_bitmap(currentSurface); }
			//Adjustment for allegro float point coordinates
			al_draw_pixel(X + 0.5, Y + 0.5, Color);
		}
		point operator+(int num){ (*this).X += num; (*this).Y += num; return (*this); }
		point operator-(int num){ (*this).X -= num; (*this).Y -= num; return (*this); }
		point operator+=(int num){ operator+(num); return (*this); }
		point operator-=(int num){ operator-(num); return (*this); }
		point operator+(point pnt){ (*this).X += pnt.X; (*this).Y += pnt.Y; return (*this); }
		point operator-(point pnt){ (*this).X -= pnt.X; (*this).Y -= pnt.Y; return (*this); }
		point operator+=(point pnt){ operator+(pnt); return (*this); }
		point operator-=(point pnt){ operator-(pnt); return (*this); }
	};
	bool operator==(point a, point b) { return a.X==b.X && a.Y==b.Y; }
	bool operator!=(point a, point b) { return !(a==b); }
	std::ostream& operator<<(std::ostream& os, const point a)
	{
  os << "(" << a.X << "," << a.Y << ")";
  return os;
	}
I think I'm misunderstanding what you need. You said you needed a function to take a vector as a parameter correct?
1
2
3
4
RETURN TYPE FUNCTION NAME(std::vector<TYPE>VECTOR NAME)
{
        return RETURN TYPE
}


Something like that? If so, in my second post there's a function exactly like that.
Topic archived. No new replies allowed.