0

I have something like this where Client and Order are classes :

  std::vector<std::pair<Client,Order>> pair;
    pair.push_back(std::make_pair(Client(2,"Anca"),Order(3,1)));
    pair.push_back(std::make_pair(Client(16,"Maria"),Order(1,3)));
    pair.push_back(std::make_pair(Client(29,"Alex"),Order(10,5)));
class Client{
private:
    int dateWhenOrderWasPlaced;
    std::string clientName;
public:
    Client(int date,std::string name){
        dateWhenOrderWasPlaced=date;
        clientName=name;
    }
class Order{
private:
    int amountPizza;
    int pizzaAge;
public:
    Order(int amPizza,int agePizza){
        amountPizza=amPizza;
        pizzaAge=agePizza;
    }

And i can't figure out how to print this.I have tried in many ways :

void print(std::vector<std::pair<Client,Order>> pair){
    for(const auto& it : pair){
        std::cout << "First: "<<pair[it].first<< ", Second: " << pair[it].second <<std::endl;
    }
}

And this :

void print(std::vector<std::pair<Client,Order>> pair){
    for(const auto& it : pair){
        std::cout << "First: "<<it.first<< ", Second: " << it.second <<std::endl;
    }
}

And in the both ways i have error(first-no operator[] and second,no operator <<)

Ac1234
  • 67
  • 6
  • 1
    You need to provide `operator<<` for `Client`, and `Order`. – cigien May 04 '20 at 23:20
  • i thought about that but can you write me how this should look ? I have no ideea .. – Ac1234 May 04 '20 at 23:22
  • See this [question](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – cigien May 04 '20 at 23:26
  • If a complex case fails, look at simpler cases to see where things break down. Are you able to print a vector of pairs of these objects? If not, are you able to print a pair of these objects? If not, can you print each of these objects individually? Don't let yourself get caught up in complexities before you have the basics working. (Looking at simpler cases also helps you write a more focused question for Stack Overflow.) – JaMiT May 04 '20 at 23:49

1 Answers1

0

Your first attempt does not work because it is the actual pair but std::pair does not have an operator[]. Your second attempt is the correct way to go, but it does not work because you have not defined operator<< for your classes.

So, simply define operator<<, eg:

class Client
{
private:
    int dateWhenOrderWasPlaced;
    std::string clientName;

public:
    Client(int date, std::string name)
        : dateWhenOrderWasPlaced(date), clientName(name)
    {
    }

    friend std::ostream& operator<<(std::ostream &os, const Client &c)
    {
        // print c.dateWhenOrderWasPlaced and c.clientName to os as needed...
        return os;
    }
};

class Order
{
private:
    int amountPizza;
    int pizzaAge;

public:
    Order(int amPizza, int agePizza)
        : amountPizza(amPizza), pizzaAge(agePizza)
    {
    }

    friend std::ostream& operator<<(std::ostream &os, const Order &o)
    {
        // print o.amountPizza and o.pizzaAge to os as needed...
        return os;
    }
};

void print(const std::vector<std::pair<Client,Order>> &pair)
{
    for(const auto& it : pair)
    {
        std::cout << "First: " << it.first << ", Second: " << it.second << std::endl;
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770