Help with passing an array of class type to a function

hi, i was hoping someone could help point me in the right direction for this problem. im writing a program that creates 3 ships and then tracks their movement; and within 10 rounds see if they colide (they are specifically set to colide at certain points).

my issue is with passing the array of ships to the function detectCollisions(), i cant get it to report a collision. I think im supposed to call the get coord on a ship object, but im not sure how to do that. any help would be awesome.

1
2
3
4
5
bool Ship::operator==( const Ship& check)
{
    return (coord == check.getCoord());  
}


1
2
3
4
5
6
7
8
9
10
11
12
13
Coordinate Ship::getCoord() const
{
    double x,y,z;
    
    x = coord.xGetter();
    y = coord.yGetter();
    z = coord.xGetter();
    
    Coordinate tempC(x,y,z);
    
    return tempC;
    
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool detectCollisions(Ship ships[], int number_of_ships)
{
  
        int check = 0;
    
        if ((ships[0] == ships[1] ) || (ships[0] == ships[2] ))
        {
            check = 1;
        }
        else if (ships[1] == ships[2]) 
        {
            check = 1;
        }
        else
        {
            check = 0;
        }
    
    return check;
}
You can use a nested loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool detectCollisions(Ship ships[], int number_of_ships)
{
	for (int i = 0; i < number_of_ships; ++i)
	{
		for (int j = i + 1; j < number_of_ships; ++j)
		{
			if (ships[i] == ships[j])
			{
				return true;
			}
		}
	}
	return false;
}



Your getCoord() function looks unnecessary complicated. Can't you just return the coord object directly?
1
2
3
4
Coordinate Ship::getCoord() const
{
	return coord;
}
Topic archived. No new replies allowed.