How use the overload of>> (or<<) of a class in the overload of << and >> of another class?

Discussion in 'C++' started by Piotre Ugrumov, Jan 24, 2004.

  1. I have done the overload on the operator >> and << in the class Attore.
    These 2 overload work correctly.
    I have done the overload of the same overload in the class Film. The class
    film ha inside an array of pointer to Attore.
    I have written these overload in these ways. the overload of << work
    correctly the overload of >> I don't know. I compile the class correctly,
    when I insert a film through the operator >> I don't receive error, but when
    I try to stamp the fil through the operator << I have an error during the
    execution of program. Why this error? Depend from the overload of >>.
    There are the overload of the operator<< and >>:
    ostream & operator<<(ostream &out, const Film &f){
    out<<f.getTitolo()<<endl;
    out<<f.getAnno()<<endl;
    for(int i=0; i<f.countAttori(); i++){
    out<<*(f.attori)<<endl; //use the overload of << in Attore, in Film I
    have defined: Attore *attori[5]; this overload works correctly
    }
    return out;
    }

    istream & operator>>(istream &in, Film &f){
    char nome[30];
    int anno;
    Attore a;
    in>>nome;
    in>>anno;
    in>>a; //I use the overload of >> in Attore;
    f.setTitolo(nome);
    f.setAnno(anno);
    f.addAttore(a);
    return in;
    }

    how can i resolve this problem?
    Thanks
    Piotre Ugrumov, Jan 24, 2004
    #1
    1. Advertising

  2. "Piotre Ugrumov" <> wrote in message
    news:1OwQb.182583$...
    > I have done the overload on the operator >> and << in the class Attore.
    > These 2 overload work correctly.
    > I have done the overload of the same overload in the class Film. The class
    > film ha inside an array of pointer to Attore.
    > I have written these overload in these ways. the overload of << work
    > correctly the overload of >> I don't know. I compile the class correctly,
    > when I insert a film through the operator >> I don't receive error, but

    when
    > I try to stamp the fil through the operator << I have an error during the
    > execution of program. Why this error? Depend from the overload of >>.
    > There are the overload of the operator<< and >>:
    > ostream & operator<<(ostream &out, const Film &f){
    > out<<f.getTitolo()<<endl;
    > out<<f.getAnno()<<endl;
    > for(int i=0; i<f.countAttori(); i++){


    problem with countAttori() perhaps?

    > out<<*(f.attori)<<endl; //use the overload of << in Attore, in Film I
    > have defined: Attore *attori[5]; this overload works correctly
    > }
    > return out;
    > }
    >
    > istream & operator>>(istream &in, Film &f){
    > char nome[30];
    > int anno;
    > Attore a;
    > in>>nome;


    This could overrun and will only get one word. Use a std::string.

    > in>>anno;


    You are not checking for success at all.

    > in>>a; //I use the overload of >> in Attore;


    You can have 5 actors but the input routine only allows 1.

    > f.setTitolo(nome);
    > f.setAnno(anno);
    > f.addAttore(a);


    does what to countAttori() ?

    > return in;
    > }
    >
    > how can i resolve this problem?
    > Thanks
    Nick Hounsome, Jan 24, 2004
    #2
    1. Advertising


  3. > > ostream & operator<<(ostream &out, const Film &f){
    > > out<<f.getTitolo()<<endl;
    > > out<<f.getAnno()<<endl;
    > > for(int i=0; i<f.countAttori(); i++){

    >
    > problem with countAttori() perhaps?




    countAttori() returns the number of Attore in the array. The max number is
    5, but in the array could be less of 5.
    I insert with the operator only 1 Attore.
    In the overload of >> in the film I have written:
    char title[30];
    in>>title;
    In this way I can give a title of only one word, with the use of string Can
    I give a title with 2 or more word?
    For insert a film I write this in the prompt:
    Goffy(title) 1990(production year) Jacques(actor name) Rosseau(actor
    surname) 11/11/1770(actor date of birth)
    but hao can a give a title with 2 or more word?
    Example:
    Goffy go to disco(title) 1990(production year) Jacques(actor name)
    Rosseau(actor surname) 11/11/1770(actor date of birth)
    Thanks.
    Piotre Ugrumov, Jan 24, 2004
    #3
  4. "Piotre Ugrumov" <> wrote in message
    news:ODxQb.182836$...
    >
    > > > ostream & operator<<(ostream &out, const Film &f){
    > > > out<<f.getTitolo()<<endl;
    > > > out<<f.getAnno()<<endl;
    > > > for(int i=0; i<f.countAttori(); i++){

    > >
    > > problem with countAttori() perhaps?

    >
    >
    >
    > countAttori() returns the number of Attore in the array. The max number is
    > 5, but in the array could be less of 5.


    I gathered that but where is the current size held and are you setting it on
    >>?


    > I insert with the operator only 1 Attore.
    > In the overload of >> in the film I have written:
    > char title[30];
    > in>>title;
    > In this way I can give a title of only one word, with the use of string

    Can
    > I give a title with 2 or more word?


    No - the only simple way is to allow a whole line
    for the title (and possibly every string field) and use getline to read it
    (preferably into a std::string).

    > For insert a film I write this in the prompt:
    > Goffy(title) 1990(production year) Jacques(actor name) Rosseau(actor
    > surname) 11/11/1770(actor date of birth)
    > but hao can a give a title with 2 or more word?
    > Example:
    > Goffy go to disco(title) 1990(production year) Jacques(actor name)
    > Rosseau(actor surname) 11/11/1770(actor date of birth)
    > Thanks.
    >
    >
    >
    Nick Hounsome, Jan 25, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Toke H?iland-J?rgensen
    Replies:
    10
    Views:
    629
    Toke H?iland-J?rgensen
    Jan 1, 2004
  2. ectoplasm
    Replies:
    12
    Views:
    619
    Zorro
    Jul 28, 2005
  3. _Who
    Replies:
    7
    Views:
    2,613
  4. Stéphane Wirtel

    Overload [] operator and use a block ?

    Stéphane Wirtel, Feb 28, 2007, in forum: Ruby
    Replies:
    4
    Views:
    89
    Stéphane Wirtel
    Feb 28, 2007
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    229
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page