I am trying to create a program that randomly generates the position of "ships". I'd like a structure to keep track of multiple aspects of the ships including their positions and an array to keep track of multiple ships.
The error I'm getting seems to be occurring in the first for loop within "main" at the line fleet[i] = ship_position (fleet[i], i);
The error reads:
error: cannot convert 'ship_stats' to 'ship_stats*' for argument '1' to 'ship_stats ship_position(ship_stats*, int)'
Also, previously, I did not think the second "i" within the brackets of that line was necessary, but when I tried compiling without it the error I received was:
error: expected primary-expression before ']' token
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int rand_range (int low, int high) {
return rand() % (high - low + 1) + low;
}
struct ship_stats {
int x_coordinate;
int y_coordinate;
int weapon_power;
};
ship_stats fleet[5]; // Create fleet of 5 ships linked to the structure
ship_stats ship_position (ship_stats fleet[], int ship_num) {
//Randomly generate a starting position for each ship
int low_x = 0; //Set max and min ranges for ship position
int high_x = 1024;
int low_y = 0;
int high_y = 768;
fleet[ship_num].x_coordinate = rand_range (low_x, high_x);
fleet[ship_num].y_coordinate = rand_range (low_y, high_y);
return fleet[ship_num];
}
int main () {
int num_ships = 5;
for (int i = 0; i < num_ships; i++)
fleet[i] = ship_position (fleet[i], i); // <-- error is here
}