-1

I read similar question for this problem but with template.

I don't have template but I the same error. This is the first time that I overload the << operator.

I have three file. A main file, a .h file and a .cpp file. The class Team use another class that I implements but I don't think that the error is caused by this class. I think that I have wrong to overload the << operator.

This is my .h file :

#include "Calciatore.h"
#include <list>

class Team{
    list<Calciatore*> calciatori;
    string nome;
    int punti;


public:
    Team();
    Team(string, int);
    Team(list<Calciatore*>, string, int);

    void set_calciatori(list<Calciatore*>);
    list<Calciatore*> get_calciatori();
    void set_calciatore(Calciatore*, int);
    Calciatore* get_calciatore(int);
    void set_nome(string);
    string get_nome() const;
    void set_punti(int);
    int get_punti() const;
    void add_calciatore(Calciatore*);

    bool operator<(Team const&); //Overload operatore < e passaggio per riferimento(riduco utilizzo memoria)
    bool operator>(Team const&);
    std::ostream& operator<<(std::ostream&);

private:
    void set_calciatori();

};

This is my .cpp file :

#include "Team.h"
#include <stdexcept>
#include <iostream>

Team::Team(){
    set_calciatori();
    set_nome("");
    set_punti(0);
}

Team::Team(string nome, int punti){
    set_calciatori();
    set_nome(nome);
    set_punti(punti);
}

Team::Team(list<Calciatore*> calciatori, string nome, int punti){
    set_calciatori(calciatori);
    set_nome(nome);
    set_punti(punti);
}

void Team::set_calciatori(list<Calciatore*> calciatori){
    if(calciatori.size() < 11 || calciatori.size() > 25){
        cout << "\nLa lista deve contenere un numero di calciatori compreso tra 11 e 25";
        return;
    }

    this->calciatori = calciatori;
}

void Team::set_calciatori(){
    for(int i = 0; i < 11; i++){
        this->calciatori.push_back(new Calciatore());
    }
}

list<Calciatore*> Team::get_calciatori(){
    return this->calciatori;
}

void Team::set_calciatore(Calciatore* calciatore, int pos){
    if(pos < 0 || pos > this->calciatori.size()){
        cout << "\nImpossibile inserire l'oggetto nella posizione inserita";
        return;
    }

    list<Calciatore*>::iterator it = calciatori.begin();
    advance(it, pos);   //Avanzo l'iteratore fino alla posizione pos
    this->calciatori.insert(it, calciatore);
}

Calciatore* Team::get_calciatore(int pos){
    if(pos < 0 || pos > this->calciatori.size()){
        cout << "\nImpossibile restituire l'oggetto alla posizione inserita";
        return NULL;
    }

    list<Calciatore*>::iterator it = calciatori.begin();
    advance(it, pos);

    return *it;
}

void Team::set_nome(string nome){
    this->nome = nome;
}

string Team::get_nome() const{
    return this->nome;
}

void Team::set_punti(int punti){
    this->punti = punti;
}

int Team::get_punti() const{
    return this->punti;
}

void Team::add_calciatore(Calciatore* calciatore){
    if(calciatori.size() >= 25){
        cout << "\nLa squadra è già al completo";
        return;
    }
    calciatori.push_back(calciatore);
}

bool Team::operator <(Team const &t){ //Ordino per punti. Se uguali guardo il nome
    if( get_punti() == t.get_punti()){
        return get_nome() < t.get_nome();
    }

    return get_punti() < t.get_punti();
}

bool Team::operator >(Team const &t){
    if( get_punti() == t.get_punti()){
        return get_nome() > t.get_nome();
    }

    return get_punti() > t.get_punti();
}

std::ostream& Team::operator<<(std::ostream& out){
    out << "\nNome : \t" << get_nome() << " \tPunti : \t" << get_punti();
    return out;
}

And this is main :

void set_Campionato(Team*[]);
void print_Campionato(Team*[]);

int main() {
    Team* serieA[20];
    set_Campionato(serieA);
    sort(serieA, serieA+20);
    print_Campionato(serieA);

    return 0;
}

void set_Campionato(Team* serieA[]){
    ...
}

void print_Campionato(Team* serieA[]){
    for(int i = 0; i < 20; i++){
        cout << *serieA[i] << "\n"; //This is line wher appears errore
    }
}

I use gcc 6.3.0. Sorry for my english and thank you for any answer.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
SGiux
  • 619
  • 3
  • 10
  • 34
  • There's a lot of irrelevant code in your question to fit for a [MCVE] that reproduces the problem you're asking for. Please remove that. – user0042 Nov 27 '17 at 20:48

1 Answers1

2
cout << *serieA[i];

is like:

operator<<(cout, *serieA[i]);

The operator<< cannot be member of your class because it takes the stream as its first parameter.

You need to make it global (non-member function) and change its parameters to stream and a reference to a Team object:

std::ostream& operator<<(std::ostream& os, Team const& team)
{ 
    // put your code here
    return os;
}

And if necessary declare it as friend in your Team class:

friend std::ostream& operator<<(std::ostream& os, Team const& team);

You can also define the friend, not just declare in the class if you want:

class Team {
...
    bool operator>(Team const&);

    friend std::ostream& operator<<(std::ostream& os, Team const& team)
    { 
        // put your code here
        return os;
    }

private:
...

A good read is operator overloading on cppreference.com

Mihayl
  • 3,821
  • 2
  • 13
  • 32
  • ok this work. Is possible implements operator<< in .h file? – SGiux Nov 28 '17 at 08:39
  • either in the class as described or you could put `inline` infront of it. – Mihayl Nov 28 '17 at 08:41
  • I'm sorry. I meant file .cpp not .h – SGiux Nov 28 '17 at 19:06
  • @Chuck94 yes. A friend declaration in a class is also a forward declaration, so you don't even need to put another `std::ostream& operator<<(std::ostream& os, Team const& team);` declaration in a header to include it with usages of `Team`. You can have the definition wherever you want, so long as there is only one. – Caleth Nov 29 '17 at 10:14