Error "No match for Operator <<" in C++

Asked

Viewed 2,029 times

1

I made this code but I have no idea why this error : "No match for 'Operator <<' In the part where I display the user’s reply... (Remembering that Namemouth is a class and Date is also...)

class Filme
   {
   private:
    string titulo;
    NomePessoa Diretor;
    NomePessoa Personagem;
    Data Lancamento;

   public:

     void getInfo(string t, string d, string p, int dia, int m, int a)
        {
            titulo = t;
            Diretor.Grava(d);
            Personagem.Grava(p);
            Lancamento.getData(dia,m,a);
        };

    void imprime()
        {
            cout << "Titulo: " << titulo << endl ;
            cout << "Nome do Diretor: " << Diretor.Imprime() << endl;
            cout << "Nome do Personagem Principal: " << Personagem.Imprime() << endl;
            cout << "Data do Lançamento: " << Lancamento.sendData() << endl;
        }; 
  • 3

    You probably need to implement the operator << in the classes NomePessoa and Data, because they should not know what to do when the operator << is used, see here: https://stackoverflow.com/a/22588202/8133067

1 answer

2

This error means that the compiler does not know how to write the object File on the given stream. This is because somewhere in your code you have something like:

Filme f;
cout << f;

How you didn’t implement the operator << for Filme compiler can’t solve this instruction because he doesn’t know how to write a Filme in the cout.

However you already have the code that shows a Filme in the method imprime and so just implement the operator << using this logic already made:

class Filme
{
   private:
    //...

   public:

     void getInfo(string t, string d, string p, int dia, int m, int a)
     {
         titulo = t;
         Diretor.Grava(d);
         Personagem.Grava(p);
         Lancamento.getData(dia,m,a);
     };

     //aqui implementação do operador <<
     friend ostream& operator << (ostream& os, const Filme& f)
     {
         //O acesso ao Filme é feito pelo segundo parametro que chamei de f
         os << "Titulo: " << f.titulo << endl ;
         os << "Nome do Diretor: " << f.Diretor.Imprime() << endl;
         os << "Nome do Personagem Principal: " << f.Personagem.Imprime() << endl;
         os << "Data do Lançamento: " << f.Lancamento.sendData() << endl;
         return os ;
     }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.