TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   overloading of operators in C++ (http://forums.techarena.in/software-development/1111458.htm)

shilong 28-01-2009 07:04 PM

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!

X-Ray 28-01-2009 10:29 PM

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.

shilong 28-01-2009 10:29 PM

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;

X-Ray 28-01-2009 10:30 PM

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.


All times are GMT +5.5. The time now is 05:55 PM.