2017-11-27 5 views
-1

이 질문에 대한 답을 템플릿과 함께 읽었습니다.'오류 :'std :: ostream {aka std :: basic_ostream <char>} 'lvalue to'std :: basic_ostream <char> && '템플릿이 없습니다.

템플릿이 없지만 같은 오류가 발생합니다. << 연산자에 과부하가 걸린 것은 이번이 처음입니다.

세 파일이 있습니다. 주 파일, .h 파일 및 .cpp 파일. 클래스 팀은 내가 구현 한 다른 클래스를 사용하지만이 클래스로 인해 오류가 발생했다고 생각하지 않습니다. 나는 < < 연산자에 과부하가 잘못되었다고 생각합니다.

#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(); 

}; 

이 내 .cpp 파일입니다 :

#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; 
} 

는 그리고이 메인입니다 :

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 
    } 
} 

내가 GCC 6.3.0를 사용

이 내 .H 파일입니다 . 영어로 죄송합니다. 답변 해 주셔서 감사합니다. 그것의 첫 번째 매개 변수로 스트림을 필요하기 때문에

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

operator<<이 클래스의 멤버가 될 수 없습니다 :

+0

A [MCVE]에 적합하기 위해 귀하의 질문에 관련이없는 코드를 많이 거기에 operator overloading입니다. 그걸 제거하십시오. – user0042

답변

2
cout << *serieA[i]; 

은 같다.

당신은 그것을 글로벌 (비 멤버 함수)를 확인하고 스트리밍하는 매개 변수와 Team 객체에 대한 참조를 변경해야

:

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

을 그리고 만약 당신 Team 수업 시간에 친구가 필요 선언을 :

friend std::ostream& operator<<(std::ostream& os, Team const& team); 
당신은 또한 당신이 원한다면 그냥 클래스에서 선언하지, 친구를 정의 할 수 있습니다

:

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

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

private: 
... 

좋은 읽기는 당신이 요구하고 문제를 재현 cppreference.com

+0

괜찮습니다. 가능 연산자 <<를 .h 파일에 구현합니까? 설명 된대로 클래스의 – Chuck94

+0

또는 인라인을 넣을 수 있습니다. –

+0

죄송합니다. 나는 .cpp 파일을 의미하지 않았다. .h – Chuck94

관련 문제