I tried to loop through the vector, but it does not work. "Invalid operands". I want to print the contents of the vector fleet1 and fleet2 but it does not work. It seems like I have to overload the class, I dont have any idea how to do this. Please advice. My class: Ship.h
#include <iostream>
#include<string>
#include <vector>
#include <memory>
#include <random>
class Ship{
public:
std::vector<Ship> fleet1, fleet2;
Ship();
void fight(Ship& target);
int RandomGenerator();
void PrintStatus();
bool tryToHit();
void chooseShip(std::vector<Ship> &fleet);
void ShipFill(vector<Ship>& fleet);
void AddShip();
};
Ship.cpp
#include <iostream>
#include "Ship.h"
void Ship::ShipFill(vector<Ship>& fleet){
Zerstoerer ZR;
Kreuzer KR;
Jaeger JR;
int input1;
//chooseShip(Shipcount1);
do {
if (fleet.size() < 9) { //not more than 9 Ships allowed
cout
<< "Which Ship do you want to add to your fleet? (1) Zerstoerer, (2) Kreuzer, (3) Jaeger or stop adding Ships (0)\n";
cin >> input1;
if (input1 == 1) { //Zerstörer
fleet.push_back(ZR);
} else if (input1 == 2) { //Kreuzer
fleet.push_back(KR);
} else if (input1 == 3) { //Jäger
fleet.push_back(JR);
}
else if(input1<0 || input1>3) {
cout << "Invalid input. Choose a Ship." << endl;
}
} else if (fleet.size() >= 9) {
cout << "Your Fleet is full. You can not choose any Ships anymore.\n";
break;
}
}while(input1 != 0);
}
void Ship::AddShip(){
int choose = 0;
while(true)
{
cout << "Do you want to adjust Fleet(1) or Fleet(2) or stop with adjusting the fleets(0)?";
cin >> choose;
switch(choose)
{
case 0: return;
case 1:
ShipFill(fleet1);
break;
case 2:
ShipFill(fleet2);
break;
default:
cout << "This Option does not exists." << endl;
break;
}
}
};
//vector<Ship> &fleet = (choose == 1) ? fleet1 : fleet2;
//ShipFill(fleet);
Tried this to loop through the vector getting the error here then: invalid operands to binary expression std::ostream(aka_basic'char') and const std::vector ''
void Ship::chooseShip( std::vector<Ship> &fleet) {
cout << "Your fleet consists of" << fleet1.size() << "ships:\n" << endl;
for (const auto &Ship: fleet) {
std::cout << Ship.getType() << " " << Ship.get_damage() << " " <<
Ship.getHull() << "x";
}
CPP file:
{
type;
hull;
size;
damage;
special;
generator = std::default_random_engine(std::random_device{}());
distribution = std::uniform_int_distribution<int>(1, 10);
};
Kreuzer::Kreuzer(){
type = "Kreuzer";
hull = 250;
size = 8;
damage = 50;
special = "Bombardement";
};
Jaeger::Jaeger(){
type = "Jaeger";
hull = 75;
size = 4;
damage = 30;
special = "CriticalHit";
};
Zerstoerer::Zerstoerer(){
type = "Zerstoerer";
hull = 160;
size = 6;
damage = 60;
special = "Zielsuchend";
};
main
std::vector<std::shared_ptr<Ship>> fleet1;
std::vector<std::shared_ptr<Ship>> fleet2;
Field obj;
obj.AddShip();```