Solved

ostream overloading << must be declared with one parameter error in compile

Posted on 2006-11-06
3
661 Views
Last Modified: 2012-05-05
i get the following error in compile,
  ostream overloading << must be declared with one parameter error in compile.

this is the program - working on date formatting in c++

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class date
{

public:

      date(int day = 1, int month = 1, int year = 1940)
{

      this->day = day;
      this->month = month;
      this->year = year;
}

date(const long& date)
{
    year = date/10000;
    if (year < 100)
    {
           year = 1900 + year;
    }
     month = (date % 10000) / 100;
     day = date % 100;
}

void Setdate(date d)
{      
               day=d.GetDay();
                month=d.GetMonth();
              year=d.GetYear();
}      
int GetDay() const
{
     return day;
}

int GetMonth() const
{
     return month;
}

int GetYear() const
     {
return year;
}

date NextDate(date d)
      {
            date ndat;

            ndat=date((d.GetDay()+1),d.GetMonth(),d.GetYear());
            ndat=date(1,(d.GetMonth()+1),d.GetYear());
            ndat = date(1,1,(d.GetYear()+1));

return ndat;
      }

ostream& operator <<(ostream& os, const date& d)
{
      return os << d;
}

void tst_print()
{
      date d;
      cout << d;
}

private:
      int day;
      int month;
      int year;
      static int numOfDays[];
};

int main(void)
{

date d(8,11,2006);

d.tst_print();

return(0);
}
0
Comment
Question by:gbilios
  • 2
3 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17887623
First of all make it friend
friend ostream& operator <<(ostream& os, const date& d);
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 130 total points
ID: 17887631
>> return os << d;
This will be recursive.
Change this to something like

friend ostream& operator <<(ostream& os, const date& d) {
   return os << d.day << "/" << d.month << "/" << d.year << endl;
}

Or, whatever format you desire.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17887867
>>>> First of all make it friend

FYI:
the main issue for doing so is *not* to have access to private members (what is just an additional benefit) *but* to tell that the overload of operator<< is not a class member but a global function. Overloads of operator<<(ostream& os, ...)  must be made global cause the return value is ostream& and not the class you define. Non-global overloads of operator<< need to return a reference to the own class. Global overloads were needed so that you can mix different outputs, e. g.

   date d = "01/01/2007";
   string s = "Next new year is: ";

   cout << s << d << endl;

Any operand here uses a different overload of operator<<

Regards, Alex
 

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question