Solved

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

Posted on 2006-11-06
3
667 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

634 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