-1

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();```
n. 1.8e9-where's-my-share m.
  • 102,958
  • 14
  • 123
  • 225
Awox
  • 1
  • 3
  • Vectors cannot be printed, unfortunately. – Quimby May 28 '22 at 15:43
  • 3
    Without knowing what the types `Zerstoerer`, `Kreuzer` and `Jaeger` are, you should be aware of [object slicing](https://stackoverflow.com/questions/274626/what-is-object-slicing) and the potential perils of placing those objects in a `std::vector` that expects to hold a `Ship`. – Drew Dormann May 28 '22 at 15:46
  • @DrewDormann I changed the function now and added the types so that you can see – Awox May 28 '22 at 16:04
  • This code should really be a [mre]. What you show here [produces lots of errors](https://godbolt.org/z/Eo1T8c8xP), but not the error you are asking about. – Drew Dormann May 28 '22 at 16:16
  • @DrewDormann there are no errors in this code except the one I mentioned – Awox May 28 '22 at 16:37
  • @Awox one error is for instance is you declare `chooseShip()` in the declaration but have `chooseShip(std::vector&)` as definition. – AndersK May 28 '22 at 16:57
  • @AndersK I am showing them I added them? – Awox May 28 '22 at 17:00
  • You should instead have a vector of unique_ptr of Ship then populate it with your various vessels. Once you have done that create a operator<< function to print a ship. – AndersK May 28 '22 at 17:00
  • ok i saw them now :) let your vector be a `std::vector>` instead, then use emplace_back and std::make_unique to populate it. – AndersK May 28 '22 at 17:01
  • @AndersK not quite familiar with this. Change the vector in the main or in the class? And will I be able to call ChooseShip(std::vector &fleet) then? main: obj.chooseShip(fleet); does not work somehow – Awox May 28 '22 at 17:30
  • Yes you just need to pass the vector by reference. `ChooseShip(std::vector> &fleet)` – AndersK May 28 '22 at 17:49
  • 1
    You cannot "overload a class". You can only overload a set of functions. Please post a [mcve]. Do not copy your entire program here, but write a completely new, *minimal* program that *reproduces* your problem. Make it fit in one source file with no include files. Make sure it does what you say it does (it is a good idea to paste it to an online compiler and verify). Then post it here *without making any changes*. – n. 1.8e9-where's-my-share m. May 28 '22 at 18:33
  • @AndersK change it in the class or in main? sorry for asking again it was not clear enough for me – Awox May 28 '22 at 18:52
  • @Awox, I see that every post you have made at Stack Overflow is followed by one or several people asking you to read what a [mre] is. Do you understand that this code is not a [mre], and the code in your question produces [a different collection of errors](https://godbolt.org/z/4qss1PGaK) because it is incomplete? You are asking us to guess what parts of your program you have omitted from the question, and that has a big impact on the quality of answers you will get. – Drew Dormann May 29 '22 at 01:15
  • why each ship contains a dumbed-down copy of a fleet or two? You're appearing to need a "self-registration pattern". – Swift - Friday Pie May 29 '22 at 08:16
  • @DrewDormann i dont know which errors for me the code works- – Awox May 29 '22 at 09:21
  • @Awox _"i dont know which errors"_ perhaps you are not clicking the links people are leaving you in the comments? Many people have sent you a link to what a [mre] is, while I sent you two links to [many errors produced by this code](https://godbolt.org/z/4qss1PGaK). That said, I notice in another question a user has suggested that you might not be reading these links. – Drew Dormann May 29 '22 at 15:25

0 Answers0