You declared operator <<
as a member function
Ship operator<<(const Ship&);
It means that the left operand is an instance of class Ship. So it could be called as
Ship a, b;
a << b;
But it is obvious that you did not want this.
if you want to output an object of class Ship in output stream then the operator has to be a non-member function. You could define it as a friend function of the class. for example
class Ship {
public:
Ship();
friend std::ostream & operator <<( std::ostream &os, const Ship& );
private:
int x = 0;
int y = 0;
std::vector<std::pair<int, int>> ship_loc; //Ship location
};
And then you could define it as for example the following way
std::ostream & operator <<( std::ostream &os, const Ship &s)
{
os << "( " << s.ship_loc[0].first << ", " << s.ship_loc[0].second << " )"
<< ", ( " << s.ship_loc[1].first << ", " << s.ship_loc[1].second << " )"
<< ", ( " << <.ship_loc[2].first << ", " << s.ship_loc[3].second << " ) "
<< ", ( " << <.ship_loc[3].first << ", " << s.ship_loc[3].second << " ) "
<< std::endl;
return os;
}
Also as the number of coordinates is fixed then there is no need to use std::vector
. Use instead std::array<std::pair<int, int>, 4>
Also remove statement
std::vector<std::pair<int, int>> ship_loc; //Ship location
from the constructor. It is a local variable. You need to fill data member ship_loc instead of this local variable.