I've been having a problem understanding why I can't write this code the way it is. So I have these class functions with the variables x and y being private and I want to check to see if the user input matches the actual location of a randomized ship. I would just change the variables to public but I'm not allowed to since this is an assignment. Any help would be nice.
class Location {
public:
Location(); // void constructor, assigns -1 to X coord, and * to Y coord
void pick(); // picks a random location
void fire(); // asks the user to input coordinates of the next shot
void print() const; // prints location in format "a1"
// predicate returns true if the two locations match
friendbool compare(const Location&, const Location&);
private:
staticconstint fieldSize = 5; // the field (ocean) is fieldSize X fieldSize
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
Compare line 9 of your class declaration with line 1 of your definition. Do you see the differences?
Your prototype at line 9 passes two arguments by const reference. Your function definition says the arguments are passed by value. This is probably going to result in a linker error indicating the proper function definition is undefined.
Also, your function definition is not going to be able to access members of Location, since it is not a friend.