Results 1 to 4 of 4

Thread: overloading of operators in C++

  1. #1
    Join Date
    Dec 2008
    Posts
    70

    overloading of operators in C++

    Hello everyone, I wanted to know if there's anyone who can help me with this problem:

    I'm writing a program in C + + in which I have defined some classes and I did the overloaded operators <<and>> for items related to these classes, but when I compile error: here you put a piece of code:

    Code:
    class data (public int G,public int M,public int A,
    public date ();
    void print (oStream & dest) 
    {dest <<G <<"/"<< M <<"/"<< A; 
    return; 
    }
    
    void read (IStream & sorg) 
    (int day, month, year; 
    char sep ='/'; 
    sorg>> day>> sep>> months>> sep>> year; 
    G = day; 
    M = month; 
    A = year; 
    return; 
    )
    )
    
    class Person: public data 
    (public: 
    string name; 
    SURNAME string; 
    birth date; 
    ADDRESS string; 
    MAIL string; 
    person (); 
    void print (oStream & dest) 
    (dest <<NAME <<endl <<NAME <<endl; 
    dest <<BIRTH; 
    dest <<endl <<ADDRESS <<endl <<MAIL <<endl <<endl; 
    return; 
    )
    
    void read (IStream & sorg) 
    (string name, surname, address, email; 
    birth date; 
    sorg>> name>> surname; 
    sorg>> birth; 
    sorg>> address>> email; 
    NAME = name; 
    SURNAME = surname; 
    BIRTH = birth; 
    ADDRESS = address; 
    MAIL = email; 
    return; 
    ) 
    )
    
    / / other classes and various robe ... 
    / / then out of the definition of any class and before opening the main, I wrote:
    oStream & operator <<(oStream & dest, const data & x) 
    (x.stampa (dest); 
    return dest; 
    ) 
    IStream & operator>> (IStream & sorg, data & x) 
    (x.leggi (sorg); 
    sorg return; 
    ) 
    oStream & operator <<(oStream & dest, const person & x) 
    (x.stampa (dest); 
    return dest; 
    ) 
    IStream & operator>> (IStream & sorg, person & x) 
    (x.leggi (sorg); 
    sorg return; 
    )
    compile time to give me this error:

    no match for 'operator <<' in 'dest <<((person) * this) -> person:: BIRTH'


    Why? How do I solve this problem?
    Thanks in advance to those who answered!

  2. #2
    Join Date
    Mar 2008
    Posts
    192

    Re: overloading of operators in C++

    The next time refers to the code, otherwise you do not understand anything.

    Code:
     class data 
      ( 
     public: 
          int G; 
          int M; 
          int A; 
          date (); 
          void print (oStream & dest) 
          ( 
              dest <<G <<"/"<< M <<"/"<< A; 
             return; 
          ) 
          void read (IStream & sorg) 
          ( 
              int day, month, year; 
              char sep ='/'; 
              sorg>> day>> sep>> months>> sep>> year; 
              G = day; 
              M = month; 
              A = year; 
             return; 
          ) 
      ) 
      class Person: public data 
      ( 
     public: 
          string name; 
          SURNAME string; 
          birth date; 
          ADDRESS string; 
          MAIL string; 
          person (); 
          void print (oStream & dest) 
          ( 
              dest <<NAME <<endl <<NAME <<endl; 
              dest <<BIRTH; 
              dest <<endl <<ADDRESS <<endl <<MAIL <<endl <<endl; 
             return; 
          ) 
          void read (IStream & sorg) 
          ( 
              string name, surname, address, email; 
              birth date; 
              sorg>> name>> surname; 
              sorg>> birth; 
              sorg>> address>> email; 
              NAME = name; 
              SURNAME = surname; 
              BIRTH = birth; 
              ADDRESS = address; 
              MAIL = email; 
             return; 
          ) 
      ) 
    
      / / other classes and various robe ... 
      / / then out of the definition of any class and before opening the main, I wrote: 
    
      oStream & operator <<(oStream & dest, const data & x) 
      ( 
          x.stampa (dest); 
          return dest; 
      ) 
      IStream & operator>> (IStream & sorg, data & x) 
      ( 
          x.leggi (sorg); 
          sorg return; 
      ) 
      oStream & operator <<(oStream & dest, const person & x) 
      ( 
          x.stampa (dest); 
          return dest; 
      ) 
      IStream & operator>> (IStream & sorg, person & x) 
      ( 
          x.leggi (sorg); 
          sorg return; 
      )
    Missing piece of code where the error, it is impossible to understand.

  3. #3
    Join Date
    Dec 2008
    Posts
    70

    Re: overloading of operators in C++

    ok, you're right ...

    the error is on this line of class "person" in the function "print":

    Code:
     dest <<BIRTH;

  4. #4
    Join Date
    Mar 2008
    Posts
    192

    Re: overloading of operators in C++

    Move those operators <<before the definition of classes.
    It is also good use never use variables State capitals. For the reading of the code will also put a space between operators and arguments.

Similar Threads

  1. What is constructor overloading in c++
    By Mast Maula in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:34 AM
  2. Method overloading c#.net
    By raju_srk in forum Software Development
    Replies: 1
    Last Post: 22-11-2010, 07:00 PM
  3. Function overloading with polymorphism
    By Mega Member in forum Software Development
    Replies: 3
    Last Post: 02-10-2009, 03:35 PM
  4. Error in Operator Overloading
    By DutchDude in forum Software Development
    Replies: 2
    Last Post: 13-05-2009, 11:54 PM
  5. Error in function overloading
    By CitricAcid in forum Software Development
    Replies: 3
    Last Post: 06-05-2009, 10:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,413,200.19704 seconds with 17 queries