Error with overload of >> and << but not only :-(((((

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

  1. I'm following your help. I have written the overload of the operator <<.
    This overload work! :)
    But I have some problem with the overload of the operator >>. I have written
    the overload of this least operator for the class Person, but I don't know
    how write the overload for a class that derived from the class Person.
    The overload of << in Person is this:
    ostream & operator<<(ostream &out, const Persona &p){
    out<<p.getNome()<<" "<<p.getCognome()<<", "<<p.getDataNascita()<<endl;
    return out;
    }
    The overload of << in class Client: Public Person:
    ostream & operator<<(ostream &out, const Cliente &p){
    out<<"Cliente: "<<endl;
    out<<(Persona)p;
    out<<"Credito: "<<p.getCredito()<<endl;
    return out;
    }

    The overload of >> in class Person is:
    istream & operator>>(istream &in, Persona &p){
    char tmp1[30];
    char tmp2[30];
    char tmp3[30];
    in>>tmp1;
    in>>tmp2;
    in>>tmp3;
    p.setName(tmp1);
    p.setSurname(tmp2);
    p.setDateOfBirth(tmp3);
    return in;
    }
    but how can I implement the overload of >> in the class Client?
    ?????

    Another thing about the overload of>>.
    If I create a new class and in this class I insert object of the class
    Client and of the class Tradar(derives from Person) how can implemente the
    overload of >>?
    The overload of << is ok, but the overload of >>? How can I implement this
    overload?

    In a class I have defined:
    Client *cPtr;
    Film *fPtr;
    Actor *aPtr;
    int csize, fsize, asize;

    I inizilize all these element to 0.
    For add an object I have created this function:
    void Archiveo::addClient(Client &c){
    Client *tmp=cPtr;
    csize = csize+1;
    cPtr=new Cliente[csize];
    for(int i=0; i<csize; i++)
    cPtr=tmp;
    cPtr[csize-1]=c;
    delete[] tmp;
    }
    The compiler don't give me any error, but when I execute a main with an
    instruction like this: a.addClient(a); all is blocked.
    How can resolve this problem?
    Thanks.
     
    Piotre Ugrumov, Jan 22, 2004
    #1
    1. Advertising

  2. Piotre Ugrumov wrote:
    >
    > I'm following your help. I have written the overload of the operator <<.
    > This overload work! :)
    > But I have some problem with the overload of the operator >>. I have written
    > the overload of this least operator for the class Person, but I don't know
    > how write the overload for a class that derived from the class Person.
    > The overload of << in Person is this:
    > ostream & operator<<(ostream &out, const Persona &p){
    > out<<p.getNome()<<" "<<p.getCognome()<<", "<<p.getDataNascita()<<endl;
    > return out;
    > }
    > The overload of << in class Client: Public Person:
    > ostream & operator<<(ostream &out, const Cliente &p){
    > out<<"Cliente: "<<endl;
    > out<<(Persona)p;
    > out<<"Credito: "<<p.getCredito()<<endl;
    > return out;
    > }
    >
    > The overload of >> in class Person is:
    > istream & operator>>(istream &in, Persona &p){
    > char tmp1[30];
    > char tmp2[30];
    > char tmp3[30];
    > in>>tmp1;
    > in>>tmp2;
    > in>>tmp3;
    > p.setName(tmp1);
    > p.setSurname(tmp2);
    > p.setDateOfBirth(tmp3);
    > return in;
    > }
    > but how can I implement the overload of >> in the class Client?
    > ?????


    In principle you do the same you did in op>> for class Person:
    You define how the user should do his input (what in which order)
    and program it that way. Of course you can use op>> for class Person
    to simplify your work.
    Eg. you could define: If a user needs to enter a Client, he has to
    provide (in that order): Name, Surname, Date of Birth, Credit

    istream& operator>> ( istream& in, Client& p )
    {
    // op >> for class Person will read name, surname, date of birth
    operator>>( in, (Persona&)p );

    int credit;
    in >> credit;

    // do whatever you need to do with credit

    return in;
    }

    >
    > Another thing about the overload of>>.
    > If I create a new class and in this class I insert object of the class
    > Client and of the class Tradar(derives from Person) how can implemente the
    > overload of >>?
    > The overload of << is ok, but the overload of >>? How can I implement this
    > overload?


    In very much the same way.
    You seem to be under the impression that there is some magic about
    operators. There is not. Operators are plain vanilla ordinary functions.
    The only magical thing about them is the way they are used.

    So ask youself: What do I need to do to read in an object of that
    type from a stream. If you can do this as ordinary function:

    istream& ReadFromStream( istream& in, Tradar& ob )
    {
    // fill the object ob with data read from the stream in

    return in;
    }

    Now replace the functions name with operator >> and you are done.

    >
    > In a class I have defined:
    > Client *cPtr;
    > Film *fPtr;
    > Actor *aPtr;
    > int csize, fsize, asize;
    >
    > I inizilize all these element to 0.
    > For add an object I have created this function:
    > void Archiveo::addClient(Client &c){
    > Client *tmp=cPtr;
    > csize = csize+1;
    > cPtr=new Cliente[csize];
    > for(int i=0; i<csize; i++)
    > cPtr=tmp;


    csize is already incremented by 1. Thus this
    loop tries to access an element from
    tmp which doesn't exist.

    > cPtr[csize-1]=c;
    > delete[] tmp;
    > }
    > The compiler don't give me any error, but when I execute a main with an
    > instruction like this: a.addClient(a); all is blocked.
    > How can resolve this problem?


    By fireing up your debugger and searching the bug.
    Or: By not doing memory management on your own. In the
    above a std::vector does all of the management under the
    hood for you. You just need to use it. And of course you
    can count on std::vector to do it right.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 22, 2004
    #2
    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. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  2. Nimmi Srivastav
    Replies:
    1
    Views:
    714
    John Harrison
    Feb 1, 2004
  3. keithb
    Replies:
    2
    Views:
    8,035
    keithb
    Jun 7, 2006
  4. David Wang
    Replies:
    0
    Views:
    741
    David Wang
    Dec 1, 2006
  5. Ying-Chieh Liao

    function overload (not operator overload)

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

Share This Page