I am trying to make a program to simulate playing Battleship in C++
#include <iostream>
#include <vector>
class Ship {
public:
Ship();
Ship operator<<(const Ship&);
private:
int x = 0;
int y = 0;
std::vector<std::pair<int, int>> ship_loc; //Ship location
};
Ship::Ship() {
srand(time(NULL));
std::vector<std::pair<int, int>> ship_loc; //Ship location
for (int i = 0; i < 4; ++i) {
x = rand() % 20;
y = rand() % 20;
std::pair<int, int> coordinates = std::make_pair(x, y);
ship_loc.push_back(coordinates);
//ship_loc.push_back(std::pair<x, y>)
};
};
Ship operator<<(const Ship &s) {
std::cout << ship_loc[0] << ship_loc[1] << ship_loc[2] << ship_loc[3]
<< std::endl;
}
int main()
{
Ship first_ship;
std::cout << first_ship;
}
Whenever I try to compile this it give me:
battleship.cpp:26:30: error: âShip operator<<(const Ship&)â must take exactly two arguments
battleship.cpp: In function âint main()â:
battleship.cpp:34:25: error: cannot bind âstd::ostream {aka std::basic_ostream<char>}â lvalue to âstd::basic_ostream<char>&&â
In file included from /usr/include/c++/4.7/iostream:40:0,
from battleship.cpp:1:
/usr/include/c++/4.7/ostream:600:5: error: initializing argument 1 of âstd::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Ship]â
Obviously not very experienced with classes yet. At all.