Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-06
3
Medium Priority
?
688 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 390 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

926 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