Question about passing by reference

Hello,

I'm trying to model a graph. I made a point class and an edge class. An edge object takes two points, and also calculates the length in between them. Now if I manipulate one point, I'd like it to be manipulated in all edges containing that specific point. I've tried passing it by reference in the constructor, but that doesn't work due to some reason. Here's my code:

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
struct point
{
	int X;
	int Y;
	bool chosen;

	//Default constructor
	point ()
	{
		X = 0;
		Y = 0;
		chosen = false;
	} 
	//Constructor with Parameters
	point (int _x, int _y)
	{
		X = _x;
		Y = _y;
		chosen = false;
	} 

	//Calculate distance between P1 and P2
	static double distance(point P1, point P2)
	{
		double temp = sqrt(pow((double)(P1.X - P2.X),2)+pow((double)(P1.Y - P2.Y),2));
		return temp;
	}
};

struct edge
{
	point P1;
	point P2;
	double length;

	edge()
	{
		P1 = point(0,0);
		P2 = point(0,0);
		length = 0;
	}
	//Construct edge and calculate the length of it
	edge (point &_p1, point &_p2)
	{
		P1 = _p1;
		P2 = _p2;
		length = point::distance(P1,P2);
	}

	static bool compare_length (edge edge1, edge edge2)
	{
		if (edge1.length == 0)
			return false;
		else
			return edge1.length < edge2.length;
	}
};

int main()
{

for (int a = 0;a<num;a++)						//Fill points vector
		{
			int x,y;
			cin >> x >> y;
			points.at(a) = point(x,y);
		}

		int k = 0;										//Iterator for the edges Vector

		for (int b = 0;b<num;b++)						//Fill the edges vector, exluding those edges which are bigger than given max
			for (int j = b+1;j<num;j++)
				if (point::distance(points.at(b), points.at(j)) <= max)
				{
					edges.at(k) = (edge(points.at(b),points.at(j)));
					k++;
				}


edges.at(0).P1.chosen = true;					
edges.at(0).P2.chosen = true;
solution.at(0) = edges.at(0);

}

(this isn't the complete code, just what's necessery to avoid clutter)


Now suppose edge i contains Point 1 and Point 2, if I mark them as chosen, then Point 2 of edge j should also be marked as chosen. But it does not do so.

How can I fix this?
Last edited on
Some help please?
Topic archived. No new replies allowed.